Trait OptionFieldRef

Source
pub trait OptionFieldRef<'x>
where Self: Copy,
{ type Input; type Output; // Required methods fn get<'a>(&'a self, obj: &'x Self::Input) -> Option<&'x Self::Output>; fn get_mut<'a>( &'a self, obj: &'x mut Self::Input, ) -> Option<&'x mut Self::Output>; // Provided method fn chain<FR, R: 'x>(&self, fr: FR) -> OptionFieldRefChain<Self, FR> where FR: OptionFieldRef<'x, Input = Self::Output, Output = R> + Copy { ... } }
Expand description

A reference to field of type Input (recursively) contained by an object of type Output and that may fail dereference.

Required Associated Types§

Required Methods§

Source

fn get<'a>(&'a self, obj: &'x Self::Input) -> Option<&'x Self::Output>

Get a reference of value in an object to which OptionFieldRef refers.

§Examples
use field_ref::{OptionFieldRef, opt_field_ref_of};

enum E {
    A(i32, char),
    B(i32)
}

let fr = opt_field_ref_of!(E::A{0});
let e1 = E::A(10, 'a');
let e2 = E::B(20);
assert_eq!(fr.get(&e1), Some(&10));
assert_eq!(fr.get(&e2), None);
Source

fn get_mut<'a>( &'a self, obj: &'x mut Self::Input, ) -> Option<&'x mut Self::Output>

Get a mutable reference of value in an object to which OptionFieldRef refers.

§Examples
use field_ref::{OptionFieldRef, opt_field_ref_of};

#[derive(Debug, Eq, PartialEq)]
enum E {
    A(i32, char),
    B(i32)
}

let fr = opt_field_ref_of!(E::B{0});
let mut e = E::B(10);
*fr.get_mut(&mut e).unwrap() = 20;
assert_eq!(e, E::B(20));

Provided Methods§

Source

fn chain<FR, R: 'x>(&self, fr: FR) -> OptionFieldRefChain<Self, FR>
where FR: OptionFieldRef<'x, Input = Self::Output, Output = R> + Copy,

Chains two field references.

§Examples
use field_ref::{OptionFieldRef, opt_field_ref_of};

enum E1 {
    A(i32),
    B(i32, char),
}

enum E2 {
    X(E1),
    Y,
}

let fr1 = opt_field_ref_of!(E2::X{0});
let fr2 = opt_field_ref_of!(E1::A{0});
let e2 = E2::X(E1::A(10));
assert_eq!(fr1.chain(fr2).get(&e2), Some(&10));

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§

Source§

impl<'x, E: 'x, T: 'x> OptionFieldRef<'x> for EnumFieldRef<E, T>

Source§

type Input = E

Source§

type Output = T

Source§

impl<'x, T, U> OptionFieldRef<'x> for FieldRefAsOptionFieldRef<T, U>

Source§

type Input = T

Source§

type Output = U

Source§

impl<'x, T: 'x, U: 'x, V: 'x, FR1, FR2> OptionFieldRef<'x> for OptionFieldRefChain<FR1, FR2>
where FR1: OptionFieldRef<'x, Input = T, Output = U>, FR2: OptionFieldRef<'x, Input = U, Output = V>,

Source§

type Input = T

Source§

type Output = V