Enum fallible_option::Fallible
source · pub enum Fallible<E> {
Success,
Fail(E),
}Expand description
Outcome of an operation that does not produce a value on success.
Variants§
Implementations§
source§impl<E> Fallible<E>
impl<E> Fallible<E>
sourcepub const fn as_deref(&self) -> Fallible<&<E as Deref>::Target>where
E: Deref,
pub const fn as_deref(&self) -> Fallible<&<E as Deref>::Target>where
E: Deref,
Converts from Fallible<E> (or &Fallible<E>) to Fallible<&E::Target>.
Leaves the original Fallible in-place, creating a new one with a reference
to the original one, additionally coercing the contents via Deref.
let fail: Fallible<String> = Fail("something went wrong".to_owned());
assert_eq!(fail.as_deref(), Fail("something went wrong"))sourcepub const fn as_deref_mut(&mut self) -> Fallible<&mut <E as Deref>::Target>where
E: DerefMut,
pub const fn as_deref_mut(&mut self) -> Fallible<&mut <E as Deref>::Target>where
E: DerefMut,
Converts from Fallible<E> (or &mut Fallible<E>) to Fallible<&mut E::Target>
Leaves the original Fallible in-place, creating a new one containing a mutable reference to
the inner type’s Deref::Target type.
let mut fail = Fail("uh oh!".to_owned());
fail.as_deref_mut().map(|err| {
err.make_ascii_uppercase();
err
});
assert_eq!(fail, Fail("UH OH!".to_owned()));sourcepub const fn as_mut(&mut self) -> Fallible<&mut E>
pub const fn as_mut(&mut self) -> Fallible<&mut E>
Converts from &mut Fallible<E> to Fallible<&mut E>
let mut fail = Fail("uh oh!".to_owned());
match fail.as_mut() {
Fail(err) => err.make_ascii_uppercase(),
Success => {}
}
assert_eq!(fail, Fail("UH OH!".to_owned()))sourcepub const fn as_ref(&self) -> Fallible<&E>
pub const fn as_ref(&self) -> Fallible<&E>
Converts from &Fallible<E> to Fallible<&E>
let fail = Fail("uh oh!");
let err_length = fail.as_ref().map(|err| err.len());
assert_eq!(err_length, Fail(6));
sourcepub const fn is_successful(&self) -> bool
pub const fn is_successful(&self) -> bool
Returns true if the value is a Success, otherwise false.
assert_eq!(Fail("some error").is_successful(), false);
assert_eq!(Success::<&str>.is_successful(), true)sourcepub const fn is_fail(&self) -> bool
pub const fn is_fail(&self) -> bool
Returns true if the value is a Fail, otherwise false.
assert_eq!(Fail("some error").is_fail(), true);
assert_eq!(Success::<&str>.is_fail(), false)sourcepub fn unwrap_fail(self) -> E
pub fn unwrap_fail(self) -> E
Unwrap the contained error or panics if no error has occurred.
let fail = Fail(70);
assert_eq!(fail.unwrap_fail(), 70);let fail: Fallible<u32> = Success;
assert_eq!(fail.unwrap_fail(), 70);sourcepub const fn contains<U: PartialEq<E>>(&self, x: &U) -> bool
pub const fn contains<U: PartialEq<E>>(&self, x: &U) -> bool
Returns true if the Fallible is a Fail value containing an error
equivalent to f
let fail = Fail("hello".to_owned());
assert!(fail.contains(&"hello"))sourcepub const fn map<F, U>(self, op: F) -> Fallible<U>where
F: FnOnce(E) -> U,
pub const fn map<F, U>(self, op: F) -> Fallible<U>where
F: FnOnce(E) -> U,
Maps an Fallible<E> to Fallible<U> by applying a function
to the contained error.
let fail = Fail("hello");
let fail = fail.map(|err| format!("{err} world!"));
assert_eq!(fail, Fail("hello world!".to_owned()));sourcepub const fn result(self) -> Result<(), E>
pub const fn result(self) -> Result<(), E>
Transforms the Fallible<E> into a Result<(), E>, where Fail(e)
becomes Err(e) and Success becomes Ok(())
let fail = Fail("error").result();
assert_eq!(fail, Err("error"));sourcepub const fn err(&self) -> Option<&E>
pub const fn err(&self) -> Option<&E>
Borrows the Fallible<E> as an Option<E>, yielding none
if no error occurred.
let fail = Fail("error occurred");
let maybe_error = fail.err();
assert_eq!(maybe_error, Some(&"error occurred"));sourcepub const fn err_or<T>(self, value: T) -> Result<T, E>
pub const fn err_or<T>(self, value: T) -> Result<T, E>
Constructs a Result<T, E> from self.
let fail: Result<u32, &str> = Fail("some error").err_or(10);
assert_eq!(fail, Err("some error"));Fail(e) becomes Err(e) and Success becomes Ok(value)
sourcepub const fn take(&mut self) -> Option<E>
pub const fn take(&mut self) -> Option<E>
Replaces the contained error (if any) with None,
and returns an Option<E> with the contained error,
if the outcome was Fail.
let mut fail = Fail("something went wrong");
let err = fail.take();
assert_eq!(fail, Success);
assert_eq!(err, Some("something went wrong"));Trait Implementations§
source§impl<E, U> FromResidual<Fallible<U>> for Fallible<E>where
E: From<U>,
impl<E, U> FromResidual<Fallible<U>> for Fallible<E>where
E: From<U>,
source§fn from_residual(residual: Fallible<U>) -> Self
fn from_residual(residual: Fallible<U>) -> Self
try_trait_v2)Residual type. Read moresource§impl<T, E, U> FromResidual<Fallible<U>> for Result<T, E>where
E: From<U>,
impl<T, E, U> FromResidual<Fallible<U>> for Result<T, E>where
E: From<U>,
source§fn from_residual(residual: Fallible<U>) -> Self
fn from_residual(residual: Fallible<U>) -> Self
try_trait_v2)Residual type. Read moresource§impl<E, U> FromResidual<Result<Infallible, U>> for Fallible<E>where
E: From<U>,
impl<E, U> FromResidual<Result<Infallible, U>> for Fallible<E>where
E: From<U>,
source§fn from_residual(residual: Result<Infallible, U>) -> Self
fn from_residual(residual: Result<Infallible, U>) -> Self
try_trait_v2)Residual type. Read moresource§impl<E: Ord> Ord for Fallible<E>
impl<E: Ord> Ord for Fallible<E>
1.21.0 · source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
source§impl<E: PartialEq> PartialEq<Fallible<E>> for Fallible<E>
impl<E: PartialEq> PartialEq<Fallible<E>> for Fallible<E>
source§impl<E: PartialOrd> PartialOrd<Fallible<E>> for Fallible<E>
impl<E: PartialOrd> PartialOrd<Fallible<E>> for Fallible<E>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self and other) and is used by the <=
operator. Read moresource§impl<E> Try for Fallible<E>
impl<E> Try for Fallible<E>
§type Output = ()
type Output = ()
try_trait_v2)? when not short-circuiting.§type Residual = Fallible<E>
type Residual = Fallible<E>
try_trait_v2)FromResidual::from_residual
as part of ? when short-circuiting. Read moresource§fn from_output(_: Self::Output) -> Self
fn from_output(_: Self::Output) -> Self
try_trait_v2)Output type. Read moresource§fn branch(self) -> ControlFlow<Self::Residual, Self::Output>
fn branch(self) -> ControlFlow<Self::Residual, Self::Output>
try_trait_v2)? to decide whether the operator should produce a value
(because this returned ControlFlow::Continue)
or propagate a value back to the caller
(because this returned ControlFlow::Break). Read more