Compactable

Trait Compactable 

Source
pub trait Compactable: Kind_cdc7cd43dac7585f {
    // Required methods
    fn compact<'a, A: 'a>(
        fa: <Self as Kind_cdc7cd43dac7585f>::Of<'a, <OptionBrand as Kind_cdc7cd43dac7585f>::Of<'a, A>>,
    ) -> <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>;
    fn separate<'a, E: 'a, O: 'a>(
        fa: <Self as Kind_cdc7cd43dac7585f>::Of<'a, Result<O, E>>,
    ) -> Pair<<Self as Kind_cdc7cd43dac7585f>::Of<'a, O>, <Self as Kind_cdc7cd43dac7585f>::Of<'a, E>>;
}
Expand description

A type class for data structures that can be compacted and separated.

Compactable allows for:

  • compact: Filtering out None values and unwrapping Some values from a structure of Options.
  • separate: Splitting a structure of Results into a pair of structures, one containing the Err values and the other containing the Ok values.

Required Methods§

Source

fn compact<'a, A: 'a>( fa: <Self as Kind_cdc7cd43dac7585f>::Of<'a, <OptionBrand as Kind_cdc7cd43dac7585f>::Of<'a, A>>, ) -> <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>

Compacts a data structure of Options, discarding None values and keeping Some values.

§Type Signature

forall a f. Compactable f => f (Option a) -> f a

§Type Parameters
  • A: The type of the elements in the Option.
§Parameters
  • fa: The data structure containing Option values.
§Returns

A new data structure containing only the values from the Some variants.

§Examples
use fp_library::classes::compactable::Compactable;
use fp_library::brands::OptionBrand;

let x = Some(Some(5));
let y = OptionBrand::compact(x);
assert_eq!(y, Some(5));

let z = Some(None::<i32>);
let w = OptionBrand::compact(z);
assert_eq!(w, None);
Source

fn separate<'a, E: 'a, O: 'a>( fa: <Self as Kind_cdc7cd43dac7585f>::Of<'a, Result<O, E>>, ) -> Pair<<Self as Kind_cdc7cd43dac7585f>::Of<'a, O>, <Self as Kind_cdc7cd43dac7585f>::Of<'a, E>>

Separates a data structure of Results into two data structures: one containing the Ok values and one containing the Err values.

§Type Signature

forall e a f. Compactable f => f (Result a e) -> (f a, f e)

§Type Parameters
  • E: The type of the error values.
  • O: The type of the success values.
§Parameters
  • fa: The data structure containing Result values.
§Returns

A pair of data structures: the first containing the Ok values, and the second containing the Err values.

§Examples
use fp_library::classes::compactable::Compactable;
use fp_library::brands::OptionBrand;
use fp_library::types::Pair;

let x: Option<Result<i32, &str>> = Some(Ok(5));
let Pair(oks, errs) = OptionBrand::separate(x);
assert_eq!(oks, Some(5));
assert_eq!(errs, None);

let y: Option<Result<i32, &str>> = Some(Err("error"));
let Pair(oks2, errs2) = OptionBrand::separate(y);
assert_eq!(oks2, None);
assert_eq!(errs2, Some("error"));

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§