Skip to main content

TrailingSliceLayout

Struct TrailingSliceLayout 

Source
pub(crate) struct TrailingSliceLayout<E = usize> {
    pub(crate) offset: usize,
    pub(crate) elem_size: E,
    pub(crate) size_base: usize,
    pub(crate) size_rounding_align_and_phase: RoundingAlignAndPhase,
}

Fields§

§offset: usize

The offset of the first byte of the trailing slice field. Note that this is NOT the same as the minimum size of the type. For example, consider the following type:

ⓘ
#[repr(C, align(2))]
struct Foo {
    header: u16,
    tag: u8,
    tail: [u8],
}

In Foo, tail is at byte offset 3. When tail.len() == 0, tail is followed by a padding byte.

§elem_size: E

The size of the element type of the trailing slice field.

§size_base: usize

The trailing slice’s byte offset plus fixed trailing padding, minus the rounding phase.

Fixed trailing padding excludes padding added by the final alignment rounding. The rounding alignment and phase are stored in size_rounding_align_and_phase. This value is independent of the slice length and can be smaller than, equal to, or larger than offset.

For a completed layout, this value is a multiple of DstLayout::align. The minimum object size equals this value when the phase is zero; otherwise, it exceeds this value by one rounding-alignment unit.

§size_rounding_align_and_phase: RoundingAlignAndPhase

The alignment and byte phase of the rounding applied to the trailing slice’s contribution to object size.

The alignment size_align is a nonzero power of two. The phase size_phase is the fixed byte contribution inside that rounding, in 0..size_align. Both are independent of the trailing slice’s length. For elems trailing elements, the enclosing object’s size is:

size_base + round_up(size_phase + elems * elem_size, size_align)

For a completed layout, size_align is at least DstLayout::align. Packing can reduce the enclosing type’s alignment and the alignment used to place its trailing field without removing padding required by a nested DST’s own layout. For example, a repr(packed(2)) wrapper around a DST with alignment 4 can have an object alignment of 2 and a rounding alignment of 4.

The rounding parameters account for padding preserved within nested fields. Their rounding origin can differ from the physical start of the trailing slice, so the phase need not equal offset % size_align.

Implementations§

Source§

impl<E> TrailingSliceLayout<E>

Source

const fn size_offset(&self) -> usize

Produces self.size_base rounded down to a multiple of size_align, plus size_phase, where both components come from self.size_rounding_align_and_phase.

This value need not equal self.offset, the trailing slice’s byte offset.

Source

const fn max_trailing_bytes(&self, available_bytes: usize) -> Option<usize>

Produces the largest trailing-slice byte capacity whose complete, padded object fits in available_bytes, or None if the object does not fit even with an empty trailing slice.

This capacity is independent of elem_size: it need not represent a whole number of elements, even for a valid Rust layout. For example, a slice of [u8; 3] elements has three-byte elements and alignment 1. With available_bytes = 4, this method produces Some(4), although only one whole element, occupying three bytes, fits.

Source§

impl TrailingSliceLayout

Source

pub(crate) const fn padding_for_elems(self, elems: usize) -> usize

Returns the difference between the complete object size and the trailing-slice end for elems elements, modulo usize::MAX + 1.

If the object size fits in usize and contains the trailing slice, this is the exact trailing padding. Other inputs need not describe a Rust type; their result is still the modular difference.

Source

pub(crate) const fn size_for_elems(self, elems: usize) -> Option<usize>

Computes the size described by this layout for elems trailing slice elements.

Returns None if the element-byte count or complete rounded size cannot be represented as a usize.

Source

pub(crate) const fn has_same_size_sequence(self, other: Self) -> bool

Returns true only when self and other describe the same size for every trailing slice length.

This recognizes sufficient conditions for equality. A false result also includes equivalent size sequences that this test cannot recognize.

Source

const fn advance(self, bytes: usize, elem_size: usize) -> Option<Self>

Advances the size formula by bytes while replacing its element size.

Let (size_align, size_phase) be decoded from self.size_rounding_align_and_phase. For a trailing slice length elems, the returned layout describes:

self.size_base
    + round_up(size_phase + bytes + elems * elem_size, size_align)

Here, elem_size is the replacement element size passed to this method. The returned layout preserves the physical slice offset self.offset and the rounding alignment; advancing changes the size calculation.

Returns None if advancing cannot produce representable normalized components. Even when those components fit, evaluating the rounded object size may overflow for some or all trailing slice lengths.

Trait Implementations§

Source§

impl<E: Clone> Clone for TrailingSliceLayout<E>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<E: Copy> Copy for TrailingSliceLayout<E>

Auto Trait Implementations§

§

impl<E> Freeze for TrailingSliceLayout<E>
where E: Freeze,

§

impl<E> RefUnwindSafe for TrailingSliceLayout<E>
where E: RefUnwindSafe,

§

impl<E> Send for TrailingSliceLayout<E>
where E: Send,

§

impl<E> Sync for TrailingSliceLayout<E>
where E: Sync,

§

impl<E> Unpin for TrailingSliceLayout<E>
where E: Unpin,

§

impl<E> UnsafeUnpin for TrailingSliceLayout<E>
where E: UnsafeUnpin,

§

impl<E> UnwindSafe for TrailingSliceLayout<E>
where E: 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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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<T> Identity for T
where T: ?Sized,

Source§

type Type = T

Self.
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<Src, Dst, A, SV, DV, R> MutationCompatible<Src, A, SV, DV, (BecauseRead, R)> for Dst
where A: Aliasing, SV: Validity, DV: Validity, Src: Read<A, R> + ?Sized, Dst: Read<A, R> + ?Sized,

Source§

impl<Src, Dst, A, SV, DV> MutationCompatible<Src, A, SV, DV, BecauseSharedCompatible> for Dst
where A: Aliasing, SV: Validity, DV: Validity, Src: TransmuteFrom<Dst, DV, SV> + ?Sized, Dst: TransmuteFrom<Src, SV, DV> + SharedCompatible<Src> + ?Sized,

Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> SharedCompatible<ManuallyDrop<T>> for T
where T: ?Sized,

Source§

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

Source§

impl<T> SharedCompatible<Unalign<T>> for T

Source§

impl<T> SharedCompatible<Wrapping<T>> for T

Source§

impl<T> SizeEq<Cell<T>> for T
where T: ?Sized,

Source§

type CastFrom = CastFromWrapper

Available on zerocopy_unstable_ptr only.
Source§

impl<T> SizeEq<ManuallyDrop<T>> for T
where T: ?Sized,

Source§

type CastFrom = CastFromWrapper

Available on zerocopy_unstable_ptr only.
Source§

impl<T> SizeEq<MaybeUninit<T>> for T

Source§

type CastFrom = CastSizedExact

Available on zerocopy_unstable_ptr only.
Source§

impl<T> SizeEq<ReadOnly<T>> for T
where T: ?Sized,

Source§

type CastFrom = CastFromReadOnly

Available on zerocopy_unstable_ptr only.
Source§

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

Source§

type CastFrom = IdCast

Available on zerocopy_unstable_ptr only.
Source§

impl<T> SizeEq<Unalign<T>> for T

Source§

type CastFrom = CastFromWrapper

Available on zerocopy_unstable_ptr only.
Source§

impl<T> SizeEq<UnsafeCell<T>> for T
where T: ?Sized,

Source§

type CastFrom = CastFromWrapper

Available on zerocopy_unstable_ptr only.
Source§

impl<T> SizeEq<Wrapping<T>> for T

Source§

type CastFrom = CastFromWrapper

Available on zerocopy_unstable_ptr only.
Source§

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

Source§

default fn lower_bound(&self) -> usize

🔬This is a nightly-only experimental API. (core_io_internals)
Returns a lower bound on the number of elements this container-like item contains. For example, an array [u8; 12] could return any value between 0 and 12 inclusively as a correct implementation. Read more
Source§

default fn upper_bound(&self) -> Option<usize>

🔬This is a nightly-only experimental API. (core_io_internals)
Returns an upper bound on the number of elements this container-like item contains if it can be determined, otherwise None. Read more
Source§

final fn size_hint(&self) -> (usize, Option<usize>)

🔬This is a nightly-only experimental API. (core_io_internals)
Returns an estimate for the number of elements this container like type contains. Read more
Source§

impl<T> Sized for T

Source§

impl<T> SizedTypeProperties for T

Source§

#[doc(hidden)]
const SIZE: usize = _

🔬This is a nightly-only experimental API. (sized_type_properties)
Source§

#[doc(hidden)]
const ALIGN: usize = _

🔬This is a nightly-only experimental API. (sized_type_properties)
Source§

#[doc(hidden)]
const ALIGNMENT: Alignment = _

🔬This is a nightly-only experimental API. (ptr_alignment_type)
Source§

#[doc(hidden)]
const IS_ZST: bool = _

🔬This is a nightly-only experimental API. (sized_type_properties)
true if this type requires no storage. false if its size is greater than zero. Read more
Source§

#[doc(hidden)]
const LAYOUT: Layout = _

🔬This is a nightly-only experimental API. (sized_type_properties)
Source§

#[doc(hidden)]
const MAX_SLICE_LEN: usize = _

🔬This is a nightly-only experimental API. (sized_type_properties)
The largest safe length for a [Self]. Read more
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> TransmuteFrom<Cell<T>, Safe, Safe> for T
where T: ?Sized,

Source§

impl<T> TransmuteFrom<ManuallyDrop<T>, Safe, Safe> for T
where T: ?Sized,

Source§

impl<T> TransmuteFrom<ReadOnly<T>, Safe, Safe> for T
where T: ?Sized,

Source§

impl<Src, Dst> TransmuteFrom<Src, Initialized, Initialized> for Dst
where Src: ?Sized, Dst: ?Sized,

Source§

impl<Src, Dst> TransmuteFrom<Src, Safe, Initialized> for Dst
where Src: IntoBytes + ?Sized, Dst: ?Sized,

Source§

impl<Src, Dst, V> TransmuteFrom<Src, V, Uninit> for Dst
where V: Validity, Src: ?Sized, Dst: ?Sized,

Source§

impl<T> TransmuteFrom<Unalign<T>, Safe, Safe> for T

Source§

impl<T> TransmuteFrom<UnsafeCell<T>, Safe, Safe> for T
where T: ?Sized,

Source§

impl<T> TransmuteFrom<Wrapping<T>, Safe, Safe> for T

Source§

impl<Src, Dst, A, SV, DV, C, R> TransmuteFromPtr<Src, A, SV, DV, C, R> for Dst
where A: Aliasing, SV: Validity, DV: Validity, C: CastExact<Src, Dst>, Dst: TransmuteFrom<Src, SV, DV> + TryTransmuteFromPtr<Src, A, SV, DV, C, R> + ?Sized, Src: ?Sized,

Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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

impl<Src, Dst, SV, DV, A, C, R> TryTransmuteFromPtr<Src, A, SV, DV, C, (BecauseMutationCompatible, R)> for Dst
where A: Aliasing, SV: Validity, DV: Validity, Src: TransmuteFrom<Dst, DV, SV> + ?Sized, Dst: MutationCompatible<Src, A, SV, DV, R> + ?Sized, C: CastExact<Src, Dst>,