Enum NullableResult

Source
pub enum NullableResult<T, E> {
    Ok(T),
    Err(E),
    Null,
}
Expand description

A replacement for Option<Result<T, E>> or Result<Option<T>, E>.

Sometimes, no value returned from an operation is not an error. It’s a special case that needs to be handled, but it’s separate from error handling. Wrapping an Option in a Result or vice versa can get very confusing very fast. Instead, use a NullableResult.

This is how it’s defined:

pub enum NullableResult<T, E> {
    Ok(T),
    Err(E),
    Null,
}

§Convert to and From std Types

It defines the From trait for Option<Result<T, E>> and for Result<Option<T>, E> in both directions, so you can easily convert between the standard library types and back.

let opt_res: Option<Result<usize, isize>> = Some(Ok(4));
let nr: NullableResult<usize, isize> = NullableResult::from(opt_res);
let opt_res: Option<Result<_,_>> = nr.into();

It also defines From for [Option<T>] and for [Result<T, E>`].

let nr: NullableResult<_, isize> = NullableResult::from(Some(4));
let result: Result<usize, isize> = Ok(4);
let nr = NullableResult::from(result);

§Unwrapping Conversions

There are also methods for unwrapping conversions. (i.e. convert to Option<T> or Result<T, E>. When converting a a Result, you need to provide an error value in case the NullableResult contains Null.

let nr = NullableResult::<usize, isize>::Ok(5);
let opt = nr.option();
let res = nr.result(-5);

There are also a few convenience methods.

let nr: NullableResult<usize, isize> = NullableResult::Ok(4);
let _ : Option<Result<usize, isize>> = nr.optional_result();
let _: Result<Option<usize>, isize> = nr.resulting_option();
let _: Result<usize, Option<isize>> = nr.result_optional_err();
let _: Result<usize, isize> = nr.result(-5); // need to provide default error value
let _: Result<usize, isize> = nr.result_with(|| 5 - 10); //closure that returns a default error

Variants§

§

Ok(T)

Contains the success value

§

Err(E)

Contains the error value

§

Null

No value

Implementations§

Source§

impl<T, E: Debug> NullableResult<T, E>

Source

pub fn unwrap(self) -> T

Panics if it’s not Ok, otherwise returns the contained value.

Source§

impl<T: Default, E> NullableResult<T, E>

Source

pub fn unwrap_or_default(self) -> T

Returns the contained value if it’s Ok, otherwise returns the default value for that type.

Source§

impl<T: Copy, E> NullableResult<&T, E>

Source

pub fn copied(self) -> NullableResult<T, E>

Returns a NullableResult with the Ok part copied.

Source§

impl<T: Copy, E> NullableResult<&mut T, E>

Source

pub fn copied(self) -> NullableResult<T, E>

Returns a NullableResult with the Ok part copied.

Source§

impl<T: Clone, E> NullableResult<&T, E>

Source

pub fn cloned(self) -> NullableResult<T, E>

Returns a NullableResult with the Ok part cloned.

Source§

impl<T: Clone, E> NullableResult<&mut T, E>

Source

pub fn cloned(self) -> NullableResult<T, E>

Returns a NullableResult with the Ok part cloned.

Source§

impl<T: Deref, E> NullableResult<T, E>

Source

pub fn as_deref(&self) -> NullableResult<&T::Target, &E>

Coerce the Ok variant of the original result with Deref and returns the new NullableResult

Source§

impl<T, E> NullableResult<T, E>

Source

pub fn is_ok(&self) -> bool

Returns true if this result is an Ok value

Source

pub fn is_err(&self) -> bool

Returns true if this result is an Err value

Source

pub fn is_null(&self) -> bool

Returns true if this result is a Null value

Source

pub fn expect(self, msg: &str) -> T

Returns the contained Ok value, consuming self.

§Panics

Panics if the value is not Ok with the provided message.

Source

pub fn unwrap_or(self, item: T) -> T

Returns the contained value if it’s Ok, returns item otherwise.

Source

pub fn unwrap_or_else<F: FnOnce() -> T>(self, f: F) -> T

Returns the contained value if it’s Ok, otherwise, it calls f and forwards its return value.

Source

pub fn option(self) -> Option<T>

Returns an Option<T> consuming self, returns Null if the NullableResult contains Err.

Source

pub fn optional_result(self) -> Option<Result<T, E>>

Return an Option<Result<T, E>> consuming self.

Source

pub fn resulting_option(self) -> Result<Option<T>, E>

Return a Result<Option<T>, E> consuming self.

Source

pub fn result(self, err: E) -> Result<T, E>

Returns a Result<T, E>, returns the provided err if the NullableResult contains Null

Source

pub fn result_with<F: FnOnce() -> E>(self, f: F) -> Result<T, E>

Returns a Result<T, E>, if the NullableResult contains Ok or Err, the value is returned, otherwise, returns the result of f.

Source

pub fn map<U, F: FnOnce(T) -> U>(self, f: F) -> NullableResult<U, E>

Maps to a NullableResult with a different ok type.

Source

pub fn map_err<U, F: FnOnce(E) -> U>(self, f: F) -> NullableResult<T, U>

Maps to a NullableResult with a different err type.

Source

pub fn result_optional_err(self) -> Result<T, Option<E>>

Returns a Result with an optional error.

Source

pub fn as_ref(&self) -> NullableResult<&T, &E>

Convert from a NullableResult<T, E> or &NullableResult<T, E> to a NullableResult<&T, &E>.

Source

pub fn as_mut(&mut self) -> NullableResult<&mut T, &mut E>

Convert from a mut NullableResult<T, E> or &mut NullableResult<T, E> to a NullableResult<&mut T, &mut E>.

Source

pub fn and<U>(self, res: NullableResult<U, E>) -> NullableResult<U, E>

If self is Ok, returns res, keeps the Err or Null from self otherwise.

Source

pub fn and_then<U, F>(self, op: F) -> NullableResult<U, E>
where F: FnOnce(T) -> NullableResult<U, E>,

Calls op if the result is Ok, otherwise returns the Err or Null from self.

Source§

impl<T, E> NullableResult<NullableResult<T, E>, E>

Source

pub fn flatten(self) -> NullableResult<T, E>

Convert from NullableResult<NullableResult<T, E>, E> to NullableResult<T, E>.

Trait Implementations§

Source§

impl<T: Clone, E: Clone> Clone for NullableResult<T, E>

Source§

fn clone(&self) -> NullableResult<T, E>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug, E: Debug> Debug for NullableResult<T, E>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T, E> Default for NullableResult<T, E>

Source§

fn default() -> Self

The default value is Null

Source§

impl<T, E> From<NullableResult<T, E>> for Option<Result<T, E>>

Source§

fn from(nr: NullableResult<T, E>) -> Self

Converts to this type from the input type.
Source§

impl<T, E> From<NullableResult<T, E>> for Result<Option<T>, E>

Source§

fn from(nr: NullableResult<T, E>) -> Self

Converts to this type from the input type.
Source§

impl<T, E> From<Option<Result<T, E>>> for NullableResult<T, E>

Source§

fn from(opt: Option<Result<T, E>>) -> Self

Converts to this type from the input type.
Source§

impl<T, E> From<Option<T>> for NullableResult<T, E>

Source§

fn from(opt: Option<T>) -> Self

Converts to this type from the input type.
Source§

impl<T, E> From<Result<Option<T>, E>> for NullableResult<T, E>

Source§

fn from(res: Result<Option<T>, E>) -> Self

Converts to this type from the input type.
Source§

impl<T, E> From<Result<T, E>> for NullableResult<T, E>

Source§

fn from(res: Result<T, E>) -> Self

Converts to this type from the input type.
Source§

impl<T, E> From<Result<T, Option<E>>> for NullableResult<T, E>

Source§

fn from(res: Result<T, Option<E>>) -> Self

Converts to this type from the input type.
Source§

impl<T, E, C> FromIterator<NullableResult<T, E>> for NullableResult<C, E>
where C: FromIterator<T>,

Source§

fn from_iter<I: IntoIterator<Item = NullableResult<T, E>>>(iter: I) -> Self

Creates a value from an iterator. Read more
Source§

impl<T: Hash, E: Hash> Hash for NullableResult<T, E>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<T: Ord, E: Ord> Ord for NullableResult<T, E>

Source§

fn cmp(&self, other: &NullableResult<T, E>) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<T: PartialEq, E: PartialEq> PartialEq for NullableResult<T, E>

Source§

fn eq(&self, other: &NullableResult<T, E>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: PartialOrd, E: PartialOrd> PartialOrd for NullableResult<T, E>

Source§

fn partial_cmp(&self, other: &NullableResult<T, E>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<T: Copy, E: Copy> Copy for NullableResult<T, E>

Source§

impl<T: Eq, E: Eq> Eq for NullableResult<T, E>

Source§

impl<T, E> StructuralPartialEq for NullableResult<T, E>

Auto Trait Implementations§

§

impl<T, E> Freeze for NullableResult<T, E>
where T: Freeze, E: Freeze,

§

impl<T, E> RefUnwindSafe for NullableResult<T, E>

§

impl<T, E> Send for NullableResult<T, E>
where T: Send, E: Send,

§

impl<T, E> Sync for NullableResult<T, E>
where T: Sync, E: Sync,

§

impl<T, E> Unpin for NullableResult<T, E>
where T: Unpin, E: Unpin,

§

impl<T, E> UnwindSafe for NullableResult<T, E>
where T: UnwindSafe, E: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T, U> MaybeTryFrom<T> for U
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type that is returned if conversion fails
Source§

fn maybe_try_from(item: T) -> NullableResult<U, <U as MaybeTryFrom<T>>::Error>

Source§

impl<T, U> MaybeTryInto<U> for T
where U: MaybeTryFrom<T>,

Source§

type Error = <U as MaybeTryFrom<T>>::Error

The type that is returned if conversion fails
Source§

fn maybe_try_into(self) -> NullableResult<U, <T as MaybeTryInto<U>>::Error>

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.