Struct FieldRef

Source
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>

Source

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

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

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

pub fn offset(&self) -> usize

Get the offset of target field.

Source

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

pub fn get_ref<'a, 'b>(&'a self, obj: &'b T) -> &'b U

👎Deprecated: Use get instead
Source

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

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

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> Clone for FieldRef<T, U>

Source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T, U> Debug for FieldRef<T, U>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl<T, U> Ord for FieldRef<T, U>

Source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<T, U> PartialEq for FieldRef<T, U>

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T, U> PartialOrd for FieldRef<T, U>

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<T, U> Copy for FieldRef<T, U>

Source§

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>

§

impl<T, U> Send for FieldRef<T, U>
where T: Send, U: Send,

§

impl<T, U> Sync for FieldRef<T, U>
where T: Sync, U: Sync,

§

impl<T, U> Unpin for FieldRef<T, U>
where T: Unpin, U: Unpin,

§

impl<T, U> UnwindSafe for FieldRef<T, U>
where T: UnwindSafe, U: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<S> GetField for S

Source§

fn get_field<T>(&self, fr: FieldRef<S, T>) -> &T

Examples Read more
Source§

fn try_get_field<'x, FR, T>(&'x self, fr: FR) -> Option<&'x T>
where FR: OptionFieldRef<'x, Input = S, Output = T>,

Source§

impl<S> GetFieldMut for S

Source§

fn get_field_mut<T>(&mut self, fr: FieldRef<S, T>) -> &mut T

Examples Read more
Source§

fn try_get_field_mut<'x, FR, T>(&'x mut self, fr: FR) -> Option<&'x mut T>
where FR: OptionFieldRef<'x, Input = S, Output = T>,

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.