forked from rust-random/rand
-
Notifications
You must be signed in to change notification settings - Fork 2
Closed
Labels
Milestone
Description
This is about design of the error type; see also #8 (should we just panic?) and #9 (error kinds).
(This assumes we do return a Result somewhere and don't do all handling via panics.)
Possible error types:
- simple kind enum
- kind enum + OS code — probably not useful (see below)
- kind + cause (wrapped error) [
stdonly] - kind +
&'static str— also not a good choice; see below
Rustdoc: https://dhardy.github.io/rand/rand_core/struct.Error.html
New proposal, which tries to be uniform between std and no_std (aside from missing cause), and allow documentation of both the current error and its cause without creating a new type.
#[derive(Debug)]
pub struct Error {
pub kind: ErrorKind,
msg: &'static str,
#[cfg(feature="std")]
cause: Option<Box<stdError + Send + Sync>>,
}
impl Error {
#[cfg(feature="std")]
pub fn new<E>(kind: ErrorKind, msg: &'static str, cause: Option<E>) -> Self
where E: Into<Box<stdError + Send + Sync>>
{
Self { kind, msg, cause: cause.map(|inner| inner.into()) }
}
#[cfg(not(feature="std"))]
pub fn new<E>(kind: ErrorKind, msg: &'static str, _cause: Option<E>) -> Self {
Self { kind, msg }
}
}Previous proposal:
#[cfg(feature="std")]
// impls: Debug, Display, ::std::error::Error
pub struct Error {
pub kind: ErrorKind,
pub cause: Option<Box<std::error::Error>,
}
#[cfg(not(feature="std"))]
// impls: Debug, Display, ::std::error::Error, Clone, PartialEq, Eq
pub struct Error {
pub kind: ErrorKind,
pub cause: Option<&'static str>,
}