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

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);

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

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