pub trait StashableResult<T, E, W, S>{
// Required methods
fn or_stash(self, stash: &mut S) -> Option<T>;
fn or_fail(self, stash: &mut S) -> Result<T, W>;
}Expand description
Required Methods§
Sourcefn or_stash(self, stash: &mut S) -> Option<T>
fn or_stash(self, stash: &mut S) -> Option<T>
Consumes this Result, returning Some(T) if this result is ok, or
collecting the error into the provided ErrorStash and returning None
if this result is an error.
This method can be used when you want to stash the result’s error but
continue execution. It will return an empty Option if an error
occurred, so you should not attempt to unwrap the result until after
checking the stash for errors.
§Example
use anyhow::{Error, anyhow};
let mut stash = BoxedStash::new();
let r1: Result<i32, Error> = Ok(1);
let r2: Result<i32, Error> = Err(anyhow!("error"));
let v1 = r1.or_stash(&mut stash);
let v2 = r2.or_stash(&mut stash);
assert_eq!(v1, Some(1));
assert_eq!(v2, None);
assert_eq!(stash.len(), 1);§Use with iterators
This method can also be used with the filter_map
method of iterators to transform an iterator of Result<T, E> into an
iterator of T, stashing any errors along the way. Note that the
StashErrorsIter trait does the same thing via a method on the
Iterator rather than on each Result.
§Example
use anyhow::{Error, anyhow};
let mut stash = BoxedStash::new();
let results = vec![Ok(1), Err(anyhow!("error")), Ok(2)];
let values: Vec<i32> = results.into_iter()
.filter_map(|r| r.or_stash(&mut stash))
.collect();
assert_eq!(values, vec![1, 2]);
assert_eq!(stash.len(), 1);Sourcefn or_fail(self, stash: &mut S) -> Result<T, W>
fn or_fail(self, stash: &mut S) -> Result<T, W>
Consumes this Result, returning Ok(T) if this result is ok, or
collecting the error into the provided ErrorStash and returning the
aggregated errors in a Err(W) if this result is an error.
This changes the result’s error from containing just the original error to containing all errors collected in the stash (including this one). It can be used when you want to return immediately if the previous operation failed, but want to include all previously collected errors in the error result as well.
Note that if this result is Ok, but the stash already contains errors,
this method will still return Ok(T). To return Err(W) if any errors
have been collected, call
fail_unless_empty on the stash after
calling this method.
§Example
use anyhow::{Error, anyhow};
let mut stash = BoxedStash::new();
stash.push(anyhow!("previous error"));
let r1: Result<i32, Error> = Ok(1);
let r2: Result<i32, Error> = Err(anyhow!("error"));
let v1 = r1.or_fail(&mut stash);
let v2 = r2.or_fail(&mut stash);
assert!(v1.is_ok());
assert_eq!(v1.unwrap(), 1);
assert!(v2.is_err());
let vec = v2.unwrap_err().to_vec();
assert_eq!(vec[0].to_string(), "previous error");
assert_eq!(vec[1].to_string(), "error");Implementations on Foreign Types§
Source§impl<T, FE> StashableResult<T, Box<dyn Error + Sync + Send>, ErrorList<Box<dyn Error + Sync + Send>>, BoxedStash> for Result<T, FE>
Lets you stash errors from a Result<T, FE> into a BoxedStash.
impl<T, FE> StashableResult<T, Box<dyn Error + Sync + Send>, ErrorList<Box<dyn Error + Sync + Send>>, BoxedStash> for Result<T, FE>
Lets you stash errors from a Result<T, FE> into a BoxedStash.
This implementation allows stashing errors from any error type FE that can be converted
into a BoxedError using the Into trait.
If the result’s error type FE is itself an ErrorList<BoxedError> (i.e.
the error type produced by another BoxedStash), its errors will be
unpacked and individually added to the target BoxedStash (rather than
nesting the entire ErrorList as a single error). In this case, summary
message from the source ErrorList will be discarded, and only its child
errors retained.
Note that this implementation leverages the Into<BoxedError> trait to auto-convert
compatible error types into BoxedError. This behavior differs from the StashableResult
implementation for TypedStash, which requires the error type to match exactly.
Source§fn or_stash(self, stash: &mut BoxedStash) -> Option<T>
fn or_stash(self, stash: &mut BoxedStash) -> Option<T>
Consumes this Result, returning Some(T) if this result is ok, or
collecting the error into the provided ErrorStash and returning None
if this result is an error.
If the error type FE is an ErrorList<BoxedError>, its individual errors
will be unpacked and added to the stash, rather than nesting the entire
ErrorList as a single error.
Source§fn or_fail(self, stash: &mut BoxedStash) -> Result<T, ErrorList<BoxedError>>
fn or_fail(self, stash: &mut BoxedStash) -> Result<T, ErrorList<BoxedError>>
Consumes this Result, returning Ok(T) if this result is ok, or
collecting the error into the provided ErrorStash and returning the
aggregated errors in a Err(W) if this result is an error.
If the error type FE is an ErrorList<BoxedError>, its individual errors
will be unpacked and added to the stash, rather than nesting the entire
ErrorList as a single error.
Source§impl<T, W, E> StashableResult<T, E, W, TypedStash<E, W>> for Result<T, Vec<E>>
Adds the ability to stash errors from a Result whose error type is a
Vec of the stash’s child error type.
impl<T, W, E> StashableResult<T, E, W, TypedStash<E, W>> for Result<T, Vec<E>>
Adds the ability to stash errors from a Result whose error type is a Vec of the stash’s child error type.
The methods of this trait implementation add all the child child errors from this result’s Vec to the stash. The ErrorList’s summary message is lost in the process- only the summary from the destination stash, if any, will be used.
The capability is currently only implemented for TypedStash, not BoxedStash,
fn or_stash(self, stash: &mut TypedStash<E, W>) -> Option<T>
fn or_fail(self, stash: &mut TypedStash<E, W>) -> Result<T, W>
Source§impl<T, W, E> StashableResult<T, E, W, TypedStash<E, W>> for Result<T, ErrorList<E>>
Adds the ability to stash errors from a Result whose error type is an
ErrorList of the stash’s child error type.
impl<T, W, E> StashableResult<T, E, W, TypedStash<E, W>> for Result<T, ErrorList<E>>
Adds the ability to stash errors from a Result whose error type is an ErrorList of the stash’s child error type.
The methods of this trait implementation add all the child child errors
from this result’s ErrorList to the stash. The ErrorList’s summary
message is lost in the process- only the summary from the destination
stash, if any, will be used.
The capability is currently only implemented for TypedStash, not BoxedStash,
fn or_stash(self, stash: &mut TypedStash<E, W>) -> Option<T>
fn or_fail(self, stash: &mut TypedStash<E, W>) -> Result<T, W>
Source§impl<T, W, E> StashableResult<T, E, W, TypedStash<E, W>> for Result<T, E>
Adds the ability to stash errors from a Result whose error type matches
the stash’s.
impl<T, W, E> StashableResult<T, E, W, TypedStash<E, W>> for Result<T, E>
Adds the ability to stash errors from a Result whose error type matches the stash’s.
Note that this implementation requires the error types of the stash and
the result to match. This differs from the StashableResult implementation
for BoxedStash, which leverages the Into<BoxedError> trait to
auto-convert compatible error types.