Enum StashedResult

Source
pub enum StashedResult<'s, T, I> {
    Ok(T),
    Err(&'s mut StashWithErrors<I>),
}
Expand description

Similar to core::result::Result, except that this type is deliberately not #[must_use] and the Err type is more or less hardcoded.

Note that the error variant is &mut StashWithErrors. When StashedResult is returned from or_stash, it actually borrows the inner value from the &mut ErrorStash that was passed to or_stash. Thus, if you want to keep the results of multiple or_stash calls around at the same time, in order to extract their Ok(t) values later, you need to call StashedResult::ok on them. Otherwise you’ll get ownership-related compilation errors. Check out StashedResult::ok for an example.

The reason we’re keeping a reference to the StashWithErrors is that it allows you to use the try2! macro (and will probably allow you use the ? operator in the future when the Try trait is stabilized).

StashedResult is returned from or_stash. There should be no need to create values of this type manually.

Variants§

§

Ok(T)

§

Err(&'s mut StashWithErrors<I>)

Implementations§

Source§

impl<'s, T, E> StashedResult<'s, T, E>

Source

pub fn ok(self) -> Option<T>

Returns Some(t) if self is Ok(t), None otherwise.

This method is useful to discard the &mut borrowing of the ErrorStash/StashWithErrors that was passed as parameter to or_stash. You may need to do this if you have multiple or_stash statements and want to extract the Ok(t) result from them later. For example, the following example would fail to compile without calling ok (due to borrowing errs mutably twice):

#[cfg(any(feature = "rust-v1.81", feature = "std"))]
use lazy_errors::{prelude::*, Result};

#[cfg(not(any(feature = "rust-v1.81", feature = "std")))]
use lazy_errors::surrogate_error_trait::{prelude::*, Result};

fn parse_version(major: &str, minor: &str) -> Result<(u32, u32)> {
    let mut errs = ErrorStash::new(|| "Invalid version number");

    let major = u32::from_str(major)
        .or_stash(&mut errs)
        .ok();

    let minor = u32::from_str(minor)
        .or_stash(&mut errs)
        .ok();

    // Return _all_ errors if major, minor, or both were invalid.
    errs.into_result()?;

    // If the result above was `Ok`, all `ok()` calls returned `Some`.
    Ok((major.unwrap(), minor.unwrap()))
}

assert_eq!(parse_version("42", "1337").unwrap(), (42, 1337));

assert_eq!(
    parse_version("42", "-1")
        .unwrap_err()
        .children()
        .len(),
    1
);

assert_eq!(
    parse_version("-1", "-1")
        .unwrap_err()
        .children()
        .len(),
    2
);

Trait Implementations§

Source§

impl<'s, T: Debug, I: Debug> Debug for StashedResult<'s, T, I>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'s, T, I> Freeze for StashedResult<'s, T, I>
where T: Freeze,

§

impl<'s, T, I> RefUnwindSafe for StashedResult<'s, T, I>

§

impl<'s, T, I> Send for StashedResult<'s, T, I>
where T: Send, I: Send,

§

impl<'s, T, I> Sync for StashedResult<'s, T, I>
where T: Sync, I: Sync,

§

impl<'s, T, I> Unpin for StashedResult<'s, T, I>
where T: Unpin,

§

impl<'s, T, I> !UnwindSafe for StashedResult<'s, T, I>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.