Skip to content

Commit

Permalink
Make choice for std/no_std error in rand_core
Browse files Browse the repository at this point in the history
  • Loading branch information
pitdicker committed Nov 1, 2017
1 parent b223c60 commit f6b3531
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
22 changes: 19 additions & 3 deletions rand_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,22 +302,38 @@ impl Error {
pub fn new(kind: ErrorKind) -> Self {
Self { kind, details: ErrorDetails::None }
}

/// Create a new instance, with a static string describing the details.
///
/// This may be used in both `std` and `no_std` enviroments.
pub fn new_str(kind: ErrorKind, details: &'static str) -> Self {
Self { kind, details: ErrorDetails::Str(details) }
}

/// Create a new instance, with a chained `Error` cause.
#[cfg(feature="std")]
pub fn new_err<E>(kind: ErrorKind, cause: E) -> Self
where E: Into<Box<std::error::Error + Send + Sync>>
{
Self { kind, details: ErrorDetails::Error(cause.into()) }
}


#[cfg(feature="std")]
pub fn new_with_cause<E>(kind: ErrorKind, _cause: E, _details: &'static str)
-> Self
where E: Into<Box<std::error::Error + Send + Sync>>
{
Error::new_str(kind, _details)
}

#[cfg(not(feature="std"))]
pub fn new_with_cause<E>(kind: ErrorKind, _cause: E, _details: &'static str)
-> Self
where E: Into<Box<std::error::Error + Send + Sync>>
{
Error::new_err(kind, _cause)
}

/// Get details on the error, if available (string message or chained
/// "cause").
///
Expand Down
12 changes: 3 additions & 9 deletions src/jitter_rng.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,16 +159,10 @@ impl ::std::error::Error for Error {
}
}

#[cfg(feature="std")]
fn new_error(kind: ErrorKind) -> rand_core::Error {
rand_core::Error::new_err(rand_core::ErrorKind::Unavailable,
Error { kind: kind })
}

#[cfg(not(feature="std"))]
fn new_error(kind: ErrorKind) -> rand_core::Error {
rand_core::Error::new_str(rand_core::ErrorKind::Unavailable,
kind.description())
rand_core::Error::new_with_cause(rand_core::ErrorKind::Unavailable,
Error { kind: kind },
kind.description())
}

impl JitterRng {
Expand Down

0 comments on commit f6b3531

Please sign in to comment.