Skip to content

Commit

Permalink
Add From<E: Error + Send> for Box<dyn Error + Send> impl
Browse files Browse the repository at this point in the history
  • Loading branch information
roblabla committed Jun 30, 2020
1 parent c86039b commit 3597d0d
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/libstd/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,43 @@ impl<'a, E: Error + 'a> From<E> for Box<dyn Error + 'a> {
}
}

#[unstable(feature = "box_error_send", issue = "none")]
impl<'a, E: Error + Send + 'a> From<E> for Box<dyn Error + Send + 'a> {
/// Converts a type of [`Error`] + [`Send`] into a box of dyn [`Error`] + [`Send`].
///
/// [`Error`]: ../error/trait.Error.html
///
/// # Examples
///
/// ```
/// use std::error::Error;
/// use std::fmt;
/// use std::mem;
///
/// #[derive(Debug)]
/// struct AnError;
///
/// impl fmt::Display for AnError {
/// fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
/// write!(f , "An error")
/// }
/// }
///
/// impl Error for AnError {}
///
/// unsafe impl Send for AnError {}
///
/// let an_error = AnError;
/// assert!(0 == mem::size_of_val(&an_error));
/// let a_boxed_error = Box::<dyn Error + Send>::from(an_error);
/// assert!(
/// mem::size_of::<Box<dyn Error + Send>>() == mem::size_of_val(&a_boxed_error))
/// ```
fn from(err: E) -> Box<dyn Error + Send + 'a> {
Box::new(err)
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<'a, E: Error + Send + Sync + 'a> From<E> for Box<dyn Error + Send + Sync + 'a> {
/// Converts a type of [`Error`] + [`Send`] + [`Sync`] into a box of
Expand Down

0 comments on commit 3597d0d

Please sign in to comment.