Skip to main content

RefAlt

Trait RefAlt 

Source
pub trait RefAlt: RefFunctor + Kind_cdc7cd43dac7585f {
    // Required method
    fn ref_alt<'a, A: 'a + Clone>(
        fa1: &<Self as Kind_cdc7cd43dac7585f>::Of<'a, A>,
        fa2: &<Self as Kind_cdc7cd43dac7585f>::Of<'a, A>,
    ) -> <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>;
}
Expand description

A type class for associative choice on type constructors, operating by reference.

RefAlt is the by-reference counterpart of Alt. Where Alt consumes its arguments, RefAlt borrows both containers and clones elements as needed to produce the result. This is useful for memoized or shared types that only expose &A access.

§Laws

RefAlt instances must satisfy the following law:

  • Associativity: ref_alt(&ref_alt(&x, &y), &z) = ref_alt(&x, &ref_alt(&y, &z)).

§Examples

RefAlt associativity for Option:

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

// Associativity: ref_alt(&ref_alt(&x, &y), &z) = ref_alt(&x, &ref_alt(&y, &z))
let x: Option<i32> = None;
let y = Some(1);
let z = Some(2);
assert_eq!(
	alt::<OptionBrand, _, _, _>(&alt::<OptionBrand, _, _, _>(&x, &y), &z),
	alt::<OptionBrand, _, _, _>(&x, &alt::<OptionBrand, _, _, _>(&y, &z)),
);

Required Methods§

Source

fn ref_alt<'a, A: 'a + Clone>( fa1: &<Self as Kind_cdc7cd43dac7585f>::Of<'a, A>, fa2: &<Self as Kind_cdc7cd43dac7585f>::Of<'a, A>, ) -> <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>

Chooses between two values in a context, operating by reference.

Both containers are borrowed. Elements are cloned as needed to construct the result.

§Type Signature

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

§Type Parameters
  • 'a: The lifetime of the values.
  • A: The type of the value inside the context.
§Parameters
  • fa1: The first value.
  • fa2: The second value.
§Returns

The chosen/combined value.

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

let x: Option<i32> = None;
let y = Some(5);
let z = alt::<OptionBrand, _, _, _>(&x, &y);
assert_eq!(z, Some(5));

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§