pub trait OrWrap<T, E> {
// Required method
fn or_wrap<I>(self) -> Result<T, Error<I>>
where E: Into<I>;
}Expand description
Adds the or_wrap method on Result<_, E>,
if E implements Into<I>.
Do not implement this trait.
Importing the trait is sufficient due to blanket implementations.
The trait is implemented on Result<_, E> if E implements Into<I>,
where I is the inner error type,
typically prelude::Stashable.
Required Methods§
Sourcefn or_wrap<I>(self) -> Result<T, Error<I>>where
E: Into<I>,
fn or_wrap<I>(self) -> Result<T, Error<I>>where
E: Into<I>,
If self is Result::Ok(value), returns Result::Ok(value);
if self is Result::Err(e1), returns Result::Err(e2)
where e2 is an Error containing a WrappedError
that will hold the original e1 value.
Allows you to convert any Result<_, E> to Result<_, Error>
if E implements Into<I>, where I is the
inner error type of Error
typically prelude::Stashable.
#[cfg(any(feature = "rust-v1.81", feature = "std"))]
use lazy_errors::prelude::*;
#[cfg(not(any(feature = "rust-v1.81", feature = "std")))]
use lazy_errors::surrogate_error_trait::prelude::*;
fn run(tokens: &[&str]) -> Result<(), Error> {
all_ascii(tokens).or_wrap()
}
fn all_ascii(tokens: &[&str]) -> Result<(), String> {
match tokens.iter().find(|s| !s.is_ascii()) {
None => Ok(()),
Some(not_ascii) => Err(not_ascii.to_string()),
}
}
fn main() {
assert!(run(&["foo", "bar"]).is_ok());
let err = run(&["foo", "❌", "bar"]).unwrap_err();
let printed = format!("{err:#}");
let printed = replace_line_numbers(&printed);
assert_eq!(printed, indoc::indoc! {"
❌
at src/or_wrap.rs:1234:56"});
}Please take a look at or_wrap_with if you’d like to
provide some kind of context information when wrapping the error.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.