#[repr(transparent)]
pub struct FieldOffset<T, U, PinFlag = NotPinned>(_, _);
Expand description

Represents a pointer to a field of type U within the type T

The PinFlag parameter can be set to AllowPin to enable the projection from Pin<&T> to Pin<&U>

Implementations§

source§

impl<T, U> FieldOffset<T, U, NotPinned>

source

pub unsafe fn new<F: for<'a> FnOnce(*const T) -> *const U>(f: F) -> Self

Construct a field offset via a lambda which returns a reference to the field in question.

Safety

The lambda must not dereference the provided pointer or access the inner value in any way as it may point to uninitialized memory.

For the returned FieldOffset to be safe to use, the returned pointer must be valid for any instance of T. For example, returning a pointer to a field from an enum with multiple variants will produce a FieldOffset which is unsafe to use.

source

pub const unsafe fn new_from_offset(offset: usize) -> Self

Construct a field offset directly from a byte offset.

Safety

For the returned FieldOffset to be safe to use, the field offset must be valid for any instance of T. For example, returning the offset to a field from an enum with multiple variants will produce a FieldOffset which is unsafe to use.

source§

impl<T, U, PinFlag> FieldOffset<T, U, PinFlag>

source

pub fn apply_ptr(self, x: *const T) -> *const U

Apply the field offset to a native pointer.

source

pub fn apply_ptr_mut(self, x: *mut T) -> *mut U

Apply the field offset to a native mutable pointer.

source

pub fn apply<'a>(self, x: &'a T) -> &'a U

Apply the field offset to a reference.

source

pub fn apply_mut<'a>(self, x: &'a mut T) -> &'a mut U

Apply the field offset to a mutable reference.

source

pub const fn get_byte_offset(self) -> usize

Get the raw byte offset for this field offset.

source

pub unsafe fn unapply_ptr(self, x: *const U) -> *const T

Unapply the field offset to a native pointer.

Safety

Warning: very unsafe!

This applies a negative offset to a pointer. If the safety implications of this are not already clear to you, then do not use this method. Also be aware that Rust has stronger aliasing rules than other languages, so it may be UB to dereference the resulting pointer even if it points to a valid location, due to the presence of other live references.

source

pub unsafe fn unapply_ptr_mut(self, x: *mut U) -> *mut T

Unapply the field offset to a native mutable pointer.

Safety

Warning: very unsafe!

This applies a negative offset to a pointer. If the safety implications of this are not already clear to you, then do not use this method. Also be aware that Rust has stronger aliasing rules than other languages, so it may be UB to dereference the resulting pointer even if it points to a valid location, due to the presence of other live references.

source

pub unsafe fn unapply<'a>(self, x: &'a U) -> &'a T

Unapply the field offset to a reference.

Safety

Warning: very unsafe!

This applies a negative offset to a reference. If the safety implications of this are not already clear to you, then do not use this method. Also be aware that Rust has stronger aliasing rules than other languages, so this method may cause UB even if the resulting reference points to a valid location, due to the presence of other live references.

source

pub unsafe fn unapply_mut<'a>(self, x: &'a mut U) -> &'a mut T

Unapply the field offset to a mutable reference.

Safety

Warning: very unsafe!

This applies a negative offset to a reference. If the safety implications of this are not already clear to you, then do not use this method. Also be aware that Rust has stronger aliasing rules than other languages, so this method may cause UB even if the resulting reference points to a valid location, due to the presence of other live references.

source

pub const unsafe fn as_pinned_projection(self) -> FieldOffset<T, U, AllowPin>

Convert this offset to an offset that is allowed to go from Pin<&T> to Pin<&U>

Safety

The Pin safety rules for projection must be respected. These rules are explained in the Pin documentation

source

pub const fn as_unpinned_projection(self) -> FieldOffset<T, U, NotPinned>

Remove the AllowPin flag

source§

impl<T, U> FieldOffset<T, U, AllowPin>

source

pub const unsafe fn new_from_offset_pinned(offset: usize) -> Self

Construct a field offset directly from a byte offset, which can be projected from a pinned.

Safety

In addition to the safety rules of FieldOffset::new_from_offset, the projection from Pin<&T> to Pin<&U> must also be allowed. The rules are explained in the Pin documentation

source

pub fn apply_pin<'a>(self, x: Pin<&'a T>) -> Pin<&'a U>

Apply the field offset to a pinned reference and return a pinned reference to the field

source

pub fn apply_pin_mut<'a>(self, x: Pin<&'a mut T>) -> Pin<&'a mut U>

Apply the field offset to a pinned mutable reference and return a pinned mutable reference to the field

Trait Implementations§

source§

impl<T, U, V> Add<FieldOffset<U, V, AllowPin>> for FieldOffset<T, U>

§

type Output = FieldOffset<T, V, NotPinned>

The resulting type after applying the + operator.
source§

fn add(self, other: FieldOffset<U, V, AllowPin>) -> FieldOffset<T, V>

Performs the + operation. Read more
source§

impl<T, U, V> Add<FieldOffset<U, V, AllowPin>> for FieldOffset<T, U, AllowPin>

§

type Output = FieldOffset<T, V, AllowPin>

The resulting type after applying the + operator.
source§

fn add(self, other: FieldOffset<U, V, AllowPin>) -> FieldOffset<T, V, AllowPin>

Performs the + operation. Read more
source§

impl<T, U, V> Add<FieldOffset<U, V, NotPinned>> for FieldOffset<T, U>

Allow chaining pointer-to-members.

Applying the resulting field offset is equivalent to applying the first field offset, then applying the second field offset.

The requirements on the generic type parameters ensure this is a safe operation.

§

type Output = FieldOffset<T, V, NotPinned>

The resulting type after applying the + operator.
source§

fn add(self, other: FieldOffset<U, V>) -> FieldOffset<T, V>

Performs the + operation. Read more
source§

impl<T, U, V> Add<FieldOffset<U, V, NotPinned>> for FieldOffset<T, U, AllowPin>

§

type Output = FieldOffset<T, V, NotPinned>

The resulting type after applying the + operator.
source§

fn add(self, other: FieldOffset<U, V>) -> FieldOffset<T, V>

Performs the + operation. Read more
source§

impl<T, U, Flag> Clone for FieldOffset<T, U, Flag>

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, Flag> Debug for FieldOffset<T, U, Flag>

The debug implementation prints the byte offset of the field in hexadecimal.

source§

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

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

impl<T, U> From<FieldOffset<T, U, AllowPin>> for FieldOffset<T, U, NotPinned>

source§

fn from(other: FieldOffset<T, U, AllowPin>) -> Self

Converts to this type from the input type.
source§

impl<T, U, Flag> Copy for FieldOffset<T, U, Flag>

Auto Trait Implementations§

§

impl<T, U, PinFlag> RefUnwindSafe for FieldOffset<T, U, PinFlag>where PinFlag: RefUnwindSafe, U: RefUnwindSafe,

§

impl<T, U, PinFlag> Send for FieldOffset<T, U, PinFlag>where PinFlag: Send, U: Send,

§

impl<T, U, PinFlag> Sync for FieldOffset<T, U, PinFlag>where PinFlag: Sync, U: Sync,

§

impl<T, U, PinFlag> Unpin for FieldOffset<T, U, PinFlag>where PinFlag: Unpin, U: Unpin,

§

impl<T, U, PinFlag> UnwindSafe for FieldOffset<T, U, PinFlag>where PinFlag: UnwindSafe, U: UnwindSafe,

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

const: unstable · source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

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

const: unstable · 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, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
source§

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

§

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

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.