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§
Sourcefn value(self) -> T
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!
Sourcefn has_value(&self) -> bool
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§
Sourcefn into_option(self) -> Option<T>where
Self: Sized,
fn into_option(self) -> Option<T>where
Self: Sized,
Converts the fallible into an option.
Returns
Some(self.value())
ifself.has_value()
is true,None
otherwise.