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<T, E> StashedResult<'_, T, E>
impl<T, E> StashedResult<'_, T, E>
Sourcepub fn ok(self) -> Option<T>
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
);