Trait Fallible

Source
pub trait Fallible<T> {
    // Required methods
    fn value(self) -> T;
    fn has_value(&self) -> bool;

    // Provided method
    fn into_option(self) -> Option<T>
       where Self: Sized { ... }
}
Expand description

Trait representing results which might be missing. This trait is useful in defining a common interface for crate::Par::filter_map inputs.

Two common examples of fallible types are Option and Result which might or not have a successful value.

§Example

use orx_parallel::*;

let success = Some(42);
assert!(success.has_value());
assert_eq!(success.value(), 42);

let success: Result<_, String> = Ok(42);
assert!(success.has_value());
assert_eq!(success.value(), 42);

let fail: Option<char> = None;
assert!(!fail.has_value());

let fail: Result<char, String> = Err("failed".to_string());
assert!(!fail.has_value());

Required Methods§

Source

fn value(self) -> T

Returns the successful value of the fallible type; panics if there is no successful value.

§Panics

Panics when value is called when has_value is false.

§Example
use orx_parallel::*;

let success = Some(42);
assert_eq!(success.value(), 42);

let success: Result<_, String> = Ok(42);
assert_eq!(success.value(), 42);

let fail: Option<char> = None;
// let _ = fail.value(); // panics!

let fail: Result<char, String> = Err("failed".to_string());
// let _ = fail.value(); // panics!
Source

fn has_value(&self) -> bool

Returns whether or not the fallible has a successful value.

§Example
use orx_parallel::*;

let success = Some(42);
assert!(success.has_value());

let success: Result<_, String> = Ok(42);
assert!(success.has_value());

let fail: Option<char> = None;
assert!(!fail.has_value());

let fail: Result<char, String> = Err("failed".to_string());
assert!(!fail.has_value());

Provided Methods§

Source

fn into_option(self) -> Option<T>
where Self: Sized,

Converts the fallible into an option.

Returns

  • Some(self.value()) if self.has_value() is true,
  • None otherwise.

Implementations on Foreign Types§

Source§

impl<T> Fallible<T> for Option<T>

Source§

fn value(self) -> T

Source§

fn has_value(&self) -> bool

Source§

impl<T, E: Debug> Fallible<T> for Result<T, E>

Source§

fn value(self) -> T

Source§

fn has_value(&self) -> bool

Implementors§