Trait field_ref::OptionFieldRef
[−]
[src]
pub trait OptionFieldRef<'x> where
Self: Copy, { type Input; type Output; 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>; fn chain<FR, R: 'x>(&self, fr: FR) -> OptionFieldRefChain<Self, FR>
where
FR: OptionFieldRef<'x, Input = Self::Output, Output = R> + Copy, { ... } }
A reference to field of type Input
(recursively) contained by an object of type Output
and that may fail dereference.
Associated Types
Required Methods
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
#[macro_use] extern crate field_ref; use field_ref::OptionFieldRef; 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);
fn get_mut<'a>(
&'a self,
obj: &'x mut Self::Input
) -> Option<&'x mut Self::Output>
&'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
#[macro_use] extern crate field_ref; use field_ref::OptionFieldRef; #[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
fn chain<FR, R: 'x>(&self, fr: FR) -> OptionFieldRefChain<Self, FR> where
FR: OptionFieldRef<'x, Input = Self::Output, Output = R> + Copy,
FR: OptionFieldRef<'x, Input = Self::Output, Output = R> + Copy,
Chains two field references.
Examples
#[macro_use] extern crate field_ref; use field_ref::OptionFieldRef; 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));
Implementors
impl<'x, E: 'x, T: 'x> OptionFieldRef<'x> for EnumFieldRef<E, T> type Input = E; type Output = T;
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>, type Input = T; type Output = V;impl<'x, T, U> OptionFieldRef<'x> for FieldRefAsOptionFieldRef<T, U> type Input = T; type Output = U;