From e45e83fc37d8f95427a29cb78a274398877f7b05 Mon Sep 17 00:00:00 2001 From: Botahamec Date: Wed, 9 Aug 2023 10:07:05 -0400 Subject: Allow RawUnexpected to be used with no alloc --- src/lib.rs | 13 +++++-------- src/result.rs | 4 ++-- src/unexpected.rs | 11 ++++++++++- 3 files changed, 17 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/lib.rs b/src/lib.rs index 2803b5d..742cc60 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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 { //! // we use `unexpect` to indicate that we don't expect this error to occur //! let num = num.parse::().unexpect()?; @@ -93,6 +95,7 @@ //! //! impl Error for NoNumberError {} //! +//! # #[cfg(feature = "std")] //! fn foo(num: Option<&str>) -> Result> { //! let num = num.ok_or(NoNumberError)?; // we expect that this may return an error //! let num = num.parse::().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> { //! // 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`] #[cfg(feature = "alloc")] pub type Expect = Exun; - -/// A type alias for [`Result`] -/// -/// [`Result`]: std::result::Result -#[cfg(feature = "alloc")] -pub type Result = core::result::Result; 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 ResultErrorExt for Result { } } -#[cfg(feature = "alloc")] +#[cfg(feature = "std")] impl ResultErrorExt for Result { fn unexpect(self) -> Self { self } } -#[cfg(feature = "alloc")] +#[cfg(feature = "std")] impl ResultErrorExt for Option { fn unexpect(self) -> Result { 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 Errorable for T {} #[derive(Debug)] enum ErrorTy { None, + #[cfg(feature = "alloc")] Message(Box), #[cfg(feature = "std")] Error(Box), @@ -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(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(error: E) -> Self { Self(RawUnexpected::msg(error)) @@ -200,12 +207,14 @@ impl From 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 for UnexpectedError { fn from(value: String) -> Self { Self(RawUnexpected::msg(value)) -- cgit v1.2.3