pub struct FieldRef<T, U> { /* private fields */ }
Expand description
A reference to field of type U
(recursively) contained by an object of type T
.
Implementations§
Source§impl<T, U> FieldRef<T, U>
impl<T, U> FieldRef<T, U>
Sourcepub unsafe fn from_offset(offset: usize) -> Self
pub unsafe fn from_offset(offset: usize) -> Self
Creates a new FieldRef
with offset bytes from the first byte of an object of type T
.
§Examples
use field_ref::FieldRef;
#[repr(C)]
struct Foo(u32, u32);
// references Foo.1
let fr = unsafe { FieldRef::<Foo, u32>::from_offset(4) };
let foo = Foo(10, 20);
assert_eq!(fr.get(&foo), &20);
Sourcepub unsafe fn from_pointers(obj: *const T, field: *const U) -> Self
pub unsafe fn from_pointers(obj: *const T, field: *const U) -> Self
Creates a new FieldRef
from a reference to concrete object of type T
and a reference to concrete field of type U
.
§Examples
use field_ref::FieldRef;
struct Foo(u32, u32, f64);
let foo1 = Foo(10, 20, 0.5);
let foo2 = Foo(30, 40, 1.5);
let fr = unsafe { FieldRef::from_pointers(&foo1, &foo1.1) };
assert_eq!(fr.get(&foo2), &40);
Sourcepub unsafe fn from_references(obj: &T, field: &U) -> Self
pub unsafe fn from_references(obj: &T, field: &U) -> Self
Creates a new FieldRef
from a pointer to concrete object of type T
and a pointer to concrete field of type U
.
§Examples
use field_ref::FieldRef;
struct Foo(u32, u32, f64);
let foo1 = Foo(10, 20, 0.5);
let foo2 = Foo(30, 40, 1.5);
let fr = unsafe { FieldRef::from_references(&foo1, &foo1.1) };
assert_eq!(fr.get(&foo2), &40);
Sourcepub fn get<'a, 'b>(&'a self, obj: &'b T) -> &'b U
pub fn get<'a, 'b>(&'a self, obj: &'b T) -> &'b U
Get a reference of value in an object to which FieldRef
refers.
§Examples
use field_ref::field_ref_of;
struct Foo(u32, u32, f64);
let fr = field_ref_of!(Foo => 1);
let foo = Foo(10, 20, 0.5);
assert_eq!(fr.get(&foo), &20);
pub fn get_ref<'a, 'b>(&'a self, obj: &'b T) -> &'b U
👎Deprecated: Use
get
insteadSourcepub fn get_mut<'a, 'b>(&'a self, obj: &'b mut T) -> &'b mut U
pub fn get_mut<'a, 'b>(&'a self, obj: &'b mut T) -> &'b mut U
Get a mutable reference of value in an object to which FieldRef
refers.
§Examples
use field_ref::field_ref_of;
struct Foo(u32, u32, f64);
let fr = field_ref_of!(Foo => 1);
let mut foo = Foo(10, 20, 0.5);
*fr.get_mut(&mut foo) = 30;
assert_eq!(foo.1, 30);
Sourcepub fn chain<V>(&self, fr: FieldRef<U, V>) -> FieldRef<T, V>
pub fn chain<V>(&self, fr: FieldRef<U, V>) -> FieldRef<T, V>
Chains two field references.
§Examples
use field_ref::field_ref_of;
struct Foo(u32, u32, f64);
struct Bar {
foo: Foo,
x: u32,
}
let fr1 = field_ref_of!(Bar => foo);
let fr2 = field_ref_of!(Foo => 1);
let bar = Bar { foo: Foo(10, 20, 0.5), x: 30 };
assert_eq!(fr1.chain(fr2).get(&bar), &20);
Sourcepub fn as_opt_field_ref(&self) -> FieldRefAsOptionFieldRef<T, U>
pub fn as_opt_field_ref(&self) -> FieldRefAsOptionFieldRef<T, U>
Convert FieldRef<T, U>
into an instance of trait OptionFieldRef<'x, Input = T, Output = U>
.
Trait Implementations§
Source§impl<T, U> Ord for FieldRef<T, U>
impl<T, U> Ord for FieldRef<T, U>
Source§impl<T, U> PartialOrd for FieldRef<T, U>
impl<T, U> PartialOrd for FieldRef<T, U>
impl<T, U> Copy for FieldRef<T, U>
impl<T, U> Eq for FieldRef<T, U>
Auto Trait Implementations§
impl<T, U> Freeze for FieldRef<T, U>
impl<T, U> RefUnwindSafe for FieldRef<T, U>where
T: RefUnwindSafe,
U: RefUnwindSafe,
impl<T, U> Send for FieldRef<T, U>
impl<T, U> Sync for FieldRef<T, U>
impl<T, U> Unpin for FieldRef<T, U>
impl<T, U> UnwindSafe for FieldRef<T, U>where
T: UnwindSafe,
U: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more