diff options
| author | Botahamec <botahamec@outlook.com> | 2023-08-09 10:07:05 -0400 |
|---|---|---|
| committer | Botahamec <botahamec@outlook.com> | 2023-08-09 10:07:05 -0400 |
| commit | e45e83fc37d8f95427a29cb78a274398877f7b05 (patch) | |
| tree | 0fe4869c5a776d9da1249b7c52b6d484555313f5 /src | |
| parent | a9f00c924fc816bd8103b4b05574f02fc4d1966f (diff) | |
Allow RawUnexpected to be used with no alloc
Diffstat (limited to 'src')
| -rw-r--r-- | src/lib.rs | 13 | ||||
| -rw-r--r-- | src/result.rs | 4 | ||||
| -rw-r--r-- | src/unexpected.rs | 11 |
3 files changed, 17 insertions, 11 deletions
@@ -4,6 +4,7 @@ #![warn(clippy::cargo)] #![allow(clippy::module_name_repetitions)] #![allow(clippy::missing_errors_doc)] + //! There are many errors we don't expect to occur. But what if we're wrong? We //! don't want our programs to panic because of that. We also don't want to spend //! so much time handling unexpected errors. That's what this crate is for. You @@ -69,6 +70,7 @@ //! ``` //! use exun::*; //! +//! # #[cfg(feature = "std")] //! fn foo(num: &str) -> Result<i32, RawUnexpected> { //! // we use `unexpect` to indicate that we don't expect this error to occur //! let num = num.parse::<i32>().unexpect()?; @@ -93,6 +95,7 @@ //! //! impl Error for NoNumberError {} //! +//! # #[cfg(feature = "std")] //! fn foo(num: Option<&str>) -> Result<i32, Expect<NoNumberError>> { //! let num = num.ok_or(NoNumberError)?; // we expect that this may return an error //! let num = num.parse::<i32>().unexpect()?; // but we think the number is otherwise parsable @@ -118,6 +121,7 @@ //! //! impl Error for NoNumberError {} //! +//! # #[cfg(feature = "std")] //! fn foo(num: Option<&str>) -> Result<i32, Exun<&str, ParseIntError>> { //! // we expect it possible to not get a number, so we handle it as such //! let num = match num { @@ -150,19 +154,12 @@ pub use result::ResultErrorExt; #[cfg(feature = "alloc")] pub use result::ResultMsgExt; -#[cfg(feature = "alloc")] -pub use unexpected::{RawUnexpected, UnexpectedError}; pub use crate::exun::Exun; pub use result::ResultExunExt; +pub use unexpected::{RawUnexpected, UnexpectedError}; pub use Exun::{Expected, Unexpected}; /// A type alias for [`Exun<E, RawUnexpected>`] #[cfg(feature = "alloc")] pub type Expect<E> = Exun<E, RawUnexpected>; - -/// A type alias for [`Result<T, RawUnexpected>`] -/// -/// [`Result<T, RawUnexpected>`]: std::result::Result -#[cfg(feature = "alloc")] -pub type Result<T> = core::result::Result<T, RawUnexpected>; diff --git a/src/result.rs b/src/result.rs index 344de5b..0bef098 100644 --- a/src/result.rs +++ b/src/result.rs @@ -66,14 +66,14 @@ impl<T, E: Error + Send + Sync + 'static> ResultErrorExt<T> for Result<T, E> { } } -#[cfg(feature = "alloc")] +#[cfg(feature = "std")] impl<T> ResultErrorExt<T> for Result<T, RawUnexpected> { fn unexpect(self) -> Self { self } } -#[cfg(feature = "alloc")] +#[cfg(feature = "std")] impl<T> ResultErrorExt<T> for Option<T> { fn unexpect(self) -> Result<T, RawUnexpected> { self.ok_or_else(RawUnexpected::none) diff --git a/src/unexpected.rs b/src/unexpected.rs index 178d01e..26cf665 100644 --- a/src/unexpected.rs +++ b/src/unexpected.rs @@ -1,7 +1,9 @@ use core::fmt::{self, Debug, Display}; -#[cfg(not(feature = "std"))] +#[cfg(all(feature = "alloc", not(feature = "std")))] use alloc::boxed::Box; +#[cfg(all(feature = "alloc", not(feature = "std")))] +use alloc::string::String; #[cfg(feature = "std")] use std::error::Error; @@ -12,6 +14,7 @@ impl<T: Display + Debug + Send + Sync + ?Sized> Errorable for T {} #[derive(Debug)] enum ErrorTy { None, + #[cfg(feature = "alloc")] Message(Box<dyn Errorable + 'static>), #[cfg(feature = "std")] Error(Box<dyn Error + Send + Sync + 'static>), @@ -32,6 +35,7 @@ impl Display for RawUnexpected { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match &self.internal { ErrorTy::None => Display::fmt("Called `unexpect` on a `None` value", f), + #[cfg(feature = "alloc")] ErrorTy::Message(m) => Display::fmt(&m, f), #[cfg(feature = "std")] ErrorTy::Error(e) => Display::fmt(&e, f), @@ -79,6 +83,7 @@ impl RawUnexpected { /// /// let x = RawUnexpected::msg("failed"); /// ``` + #[cfg(feature = "alloc")] #[must_use] pub fn msg<E: Display + Debug + Send + Sync + 'static>(error: E) -> Self { Self { @@ -126,6 +131,7 @@ impl RawUnexpected { pub fn source(&self) -> Option<&(dyn Error + 'static)> { match &self.internal { ErrorTy::None => None, + #[cfg(feature = "alloc")] ErrorTy::Message(_) => None, #[cfg(feature = "std")] ErrorTy::Error(e) => Some(&**e), @@ -171,6 +177,7 @@ impl UnexpectedError { /// /// let x = UnexpectedError::msg("failed"); /// ``` + #[cfg(feature = "alloc")] #[must_use] pub fn msg<E: Display + Debug + Send + Sync + 'static>(error: E) -> Self { Self(RawUnexpected::msg(error)) @@ -200,12 +207,14 @@ impl From<RawUnexpected> for UnexpectedError { } } +#[cfg(feature = "alloc")] impl From<&'static str> for UnexpectedError { fn from(value: &'static str) -> Self { Self(RawUnexpected::msg(value)) } } +#[cfg(feature = "alloc")] impl From<String> for UnexpectedError { fn from(value: String) -> Self { Self(RawUnexpected::msg(value)) |
