pub enum Fallible<E> {
    Success,
    Fail(E),
}
Expand description

Outcome of an operation that does not produce a value on success.

Variants§

§

Success

No error was produced.

§

Fail(E)

An error was produced.

Implementations§

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"))

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()));

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()))

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));

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)

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)

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);

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"))

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()));

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"));

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"));

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)

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"));

Maps a Fallible<&E> to a Fallible<E> by cloning the contents of the error.

Maps an Fallible<&E> to an Fallible<E> by copying the contents of the error.

Maps an Fallible<&E> to an Fallible<E> by cloning the contents of the error.

Maps an Fallible<&E> to an Fallible<E> by copying the contents of the error.

The following functions are only available if the generic parameter E implements Debug

Returns a unit value if the Fallible is not Fail.

Panics

Panics if the value is a Fail, with a panic message including the content of the Fail.

let success: Fallible<()> = Success;
success.unwrap();
let fail = Fail("hello world");
fail.unwrap();

Flattens a Fallible<Fallible<E>> into a Fallible<E>

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Returns the “default value” for a type. Read more
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
🔬This is a nightly-only experimental API. (try_trait_v2)
Constructs the type from a compatible Residual type. Read more
🔬This is a nightly-only experimental API. (try_trait_v2)
Constructs the type from a compatible Residual type. Read more
🔬This is a nightly-only experimental API. (try_trait_v2)
Constructs the type from a compatible Residual type. Read more
🔬This is a nightly-only experimental API. (try_trait_v2)
Constructs the type from a compatible Residual type. Read more
Feeds this value into the given Hasher. Read more
Feeds a slice of this type into the given Hasher. Read more
This method returns an Ordering between self and other. Read more
Compares and returns the maximum of two values. Read more
Compares and returns the minimum of two values. Read more
Restrict a value to a certain interval. Read more
This method tests for self and other values to be equal, and is used by ==.
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
This method returns an ordering between self and other values if one exists. Read more
This method tests less than (for self and other) and is used by the < operator. Read more
This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
This method tests greater than (for self and other) and is used by the > operator. Read more
This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
🔬This is a nightly-only experimental API. (try_trait_v2)
The type of the value produced by ? when not short-circuiting.
🔬This is a nightly-only experimental API. (try_trait_v2)
The type of the value passed to FromResidual::from_residual as part of ? when short-circuiting. Read more
🔬This is a nightly-only experimental API. (try_trait_v2)
Constructs the type from its Output type. Read more
🔬This is a nightly-only experimental API. (try_trait_v2)
Used in ? 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

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more
Converts to this type from the input type.

Returns the argument unchanged.

Calls U::from(self).

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

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.