Skip to main content

RefCompactable

Trait RefCompactable 

Source
pub trait RefCompactable: Kind_cdc7cd43dac7585f {
    // Required methods
    fn ref_compact<'a, A: 'a + Clone>(
        fa: &<Self as Kind_cdc7cd43dac7585f>::Of<'a, Option<A>>,
    ) -> <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>;
    fn ref_separate<'a, E: 'a + Clone, O: 'a + Clone>(
        fa: &<Self as Kind_cdc7cd43dac7585f>::Of<'a, Result<O, E>>,
    ) -> (<Self as Kind_cdc7cd43dac7585f>::Of<'a, E>, <Self as Kind_cdc7cd43dac7585f>::Of<'a, O>);
}
Expand description

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

Like Compactable, but takes the container by reference (&F<Option<A>>) instead of by value. Because elements must be extracted from a borrowed container, the element types require Clone.

§Laws

The same laws as Compactable apply; ref_compact and ref_separate must agree with their by-value counterparts when applied to cloned inputs.

§Examples

use fp_library::{
	brands::*,
	functions::explicit::*,
};

let v = vec![Some(1), None, Some(3)];
let result = compact::<VecBrand, _, _, _>(&v);
assert_eq!(result, vec![1, 3]);

let v2: Vec<Result<i32, &str>> = vec![Ok(1), Err("bad"), Ok(3)];
let (errs, oks) = separate::<VecBrand, _, _, _, _>(&v2);
assert_eq!(oks, vec![1, 3]);
assert_eq!(errs, vec!["bad"]);

Required Methods§

Source

fn ref_compact<'a, A: 'a + Clone>( fa: &<Self as Kind_cdc7cd43dac7585f>::Of<'a, Option<A>>, ) -> <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>

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

§Type Signature

forall A. &Self (Option A) -> Self A

§Type Parameters
  • 'a: The lifetime of the elements.
  • A: The type of the elements in the Option. Must be Clone because elements are extracted from a borrowed container.
§Parameters
  • fa: A reference to the data structure containing Option values.
§Returns

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

§Examples
use fp_library::{
	brands::*,
	functions::explicit::*,
};

let v = vec![Some(1), None, Some(3)];
let result = compact::<VecBrand, _, _, _>(&v);
assert_eq!(result, vec![1, 3]);

let v2 = vec![None::<i32>, None, None];
let result2 = compact::<VecBrand, _, _, _>(&v2);
assert_eq!(result2, Vec::<i32>::new());
Source

fn ref_separate<'a, E: 'a + Clone, O: 'a + Clone>( fa: &<Self as Kind_cdc7cd43dac7585f>::Of<'a, Result<O, E>>, ) -> (<Self as Kind_cdc7cd43dac7585f>::Of<'a, E>, <Self as Kind_cdc7cd43dac7585f>::Of<'a, O>)

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

§Type Signature

forall E O. &Self (Result O E) -> (Self E, Self O)

§Type Parameters
  • 'a: The lifetime of the elements.
  • E: The type of the error values. Must be Clone because elements are extracted from a borrowed container.
  • O: The type of the success values. Must be Clone because elements are extracted from a borrowed container.
§Parameters
  • fa: A reference to the data structure containing Result values.
§Returns

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

§Examples
use fp_library::{
	brands::*,
	functions::explicit::*,
};

let v: Vec<Result<i32, &str>> = vec![Ok(1), Err("bad"), Ok(3)];
let (errs, oks) = separate::<VecBrand, _, _, _, _>(&v);
assert_eq!(oks, vec![1, 3]);
assert_eq!(errs, vec!["bad"]);

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§