summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorBotahamec <botahamec@outlook.com>2022-10-23 15:57:23 -0400
committerBotahamec <botahamec@outlook.com>2022-10-23 15:57:23 -0400
commitf27518b60bb697000dcb1f6363066ddbaf505368 (patch)
tree116a838ccb20c56eda6eaefb1a63fe60f969620d /src
parented3203298032bb124d309f19dab8926ed5a95bd2 (diff)
Added more constructors
Diffstat (limited to 'src')
-rw-r--r--src/exun.rs3
-rw-r--r--src/unexpected.rs48
2 files changed, 45 insertions, 6 deletions
diff --git a/src/exun.rs b/src/exun.rs
index 9e9e1f2..d2ef1eb 100644
--- a/src/exun.rs
+++ b/src/exun.rs
@@ -4,8 +4,7 @@ use core::fmt::{self, Debug, Display};
use std::error::Error;
#[cfg(feature = "alloc")]
-use crate::RawUnexpected;
-use crate::UnexpectedError;
+use crate::{RawUnexpected, UnexpectedError};
pub use Exun::{Expected, Unexpected};
diff --git a/src/unexpected.rs b/src/unexpected.rs
index ed99e1f..321973f 100644
--- a/src/unexpected.rs
+++ b/src/unexpected.rs
@@ -60,9 +60,9 @@ impl RawUnexpected {
/// let x = RawUnexpected::new(core::fmt::Error);
/// ```
#[cfg(feature = "std")]
- pub fn new<E: Error + Send + Sync + 'static>(e: E) -> Self {
+ pub fn new<E: Error + Send + Sync + 'static>(error: E) -> Self {
Self {
- internal: ErrorTy::Error(Box::new(e)),
+ internal: ErrorTy::Error(Box::new(error)),
}
}
@@ -80,9 +80,9 @@ impl RawUnexpected {
///
/// let x = RawUnexpected::msg("failed");
/// ```
- pub fn msg<E: Display + Debug + Send + Sync + 'static>(e: E) -> Self {
+ pub fn msg<E: Display + Debug + Send + Sync + 'static>(error: E) -> Self {
Self {
- internal: ErrorTy::Message(Box::new(e)),
+ internal: ErrorTy::Message(Box::new(error)),
}
}
@@ -105,6 +105,7 @@ impl RawUnexpected {
/// assert!(x.source().is_none());
/// ```
#[must_use]
+ #[cfg(feature = "std")]
pub fn source(&self) -> Option<&(dyn Error + 'static)> {
match &self.internal {
ErrorTy::Message(_) => None,
@@ -121,6 +122,45 @@ impl RawUnexpected {
#[derive(Debug)]
pub struct UnexpectedError(RawUnexpected);
+impl UnexpectedError {
+ /// Create a new `UnexpectedError` from any [`Error`] type.
+ ///
+ /// The error must be thread-safe and `'static` so that the
+ /// `UnexpectedError` will be too.
+ ///
+ /// # Examples
+ ///
+ /// Basic usage:
+ ///
+ /// ```
+ /// use exun::*;
+ ///
+ /// let x = UnexpectedError::new(core::fmt::Error);
+ /// ```
+ #[cfg(feature = "std")]
+ pub fn new<E: Error + Send + Sync + 'static>(error: E) -> Self {
+ Self(RawUnexpected::new(error))
+ }
+
+ /// Create a new `UnexpectedError` from a printable error message.
+ ///
+ /// If the argument implements [`Error`], prefer [`UnexpectedError::new`]
+ /// instead, which preserves the source.
+ ///
+ /// # Examples
+ ///
+ /// Basic usage:
+ ///
+ /// ```
+ /// use exun::*;
+ ///
+ /// let x = UnexpectedError::msg("failed");
+ /// ```
+ pub fn msg<E: Display + Debug + Send + Sync + 'static>(error: E) -> Self {
+ Self(RawUnexpected::msg(error))
+ }
+}
+
impl From<RawUnexpected> for UnexpectedError {
fn from(ru: RawUnexpected) -> Self {
Self(ru)