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§
Sourcefn get<'a>(&'a self, obj: &'x Self::Input) -> Option<&'x Self::Output>
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);
Sourcefn get_mut<'a>(
&'a self,
obj: &'x mut Self::Input,
) -> Option<&'x mut Self::Output>
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§
Sourcefn chain<FR, R: 'x>(&self, fr: FR) -> OptionFieldRefChain<Self, FR>
fn chain<FR, R: 'x>(&self, fr: FR) -> OptionFieldRefChain<Self, FR>
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.