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: usizeThe 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: EThe size of the element type of the trailing slice field.
size_base: usizeThe 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: RoundingAlignAndPhaseThe 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>
impl<E> TrailingSliceLayout<E>
Sourceconst fn size_offset(&self) -> usize
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.
Sourceconst fn max_trailing_bytes(&self, available_bytes: usize) -> Option<usize>
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
impl TrailingSliceLayout
Sourcepub(crate) const fn padding_for_elems(self, elems: usize) -> usize
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.
Sourcepub(crate) const fn size_for_elems(self, elems: usize) -> Option<usize>
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.
Sourcepub(crate) const fn has_same_size_sequence(self, other: Self) -> bool
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.
Sourceconst fn advance(self, bytes: usize, elem_size: usize) -> Option<Self>
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>
impl<E: Clone> Clone for TrailingSliceLayout<E>
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> 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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<Src, Dst, A, SV, DV, R> MutationCompatible<Src, A, SV, DV, (BecauseRead, R)> for Dst
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<T> SizeEq<ManuallyDrop<T>> for Twhere
T: ?Sized,
impl<T> SizeEq<ManuallyDrop<T>> for Twhere
T: ?Sized,
Source§impl<T> SizeEq<MaybeUninit<T>> for T
impl<T> SizeEq<MaybeUninit<T>> for T
Source§type CastFrom = CastSizedExact
type CastFrom = CastSizedExact
zerocopy_unstable_ptr only.Source§impl<T> SizeEq<UnsafeCell<T>> for Twhere
T: ?Sized,
impl<T> SizeEq<UnsafeCell<T>> for Twhere
T: ?Sized,
Source§impl<T> SizeHint for Twhere
T: ?Sized,
impl<T> SizeHint for Twhere
T: ?Sized,
Source§default fn lower_bound(&self) -> usize
default fn lower_bound(&self) -> usize
core_io_internals)[u8; 12] could return any value between 0 and
12 inclusively as a correct implementation. Read moreimpl<T> Sized for T
Source§impl<T> SizedTypeProperties for T
impl<T> SizedTypeProperties for T
Source§#[doc(hidden)]const SIZE: usize = _
#[doc(hidden)]const SIZE: usize = _
sized_type_properties)Source§#[doc(hidden)]const ALIGN: usize = _
#[doc(hidden)]const ALIGN: usize = _
sized_type_properties)Source§#[doc(hidden)]const ALIGNMENT: Alignment = _
#[doc(hidden)]const ALIGNMENT: Alignment = _
ptr_alignment_type)Source§#[doc(hidden)]const IS_ZST: bool = _
#[doc(hidden)]const IS_ZST: bool = _
sized_type_properties)Source§#[doc(hidden)]const LAYOUT: Layout = _
#[doc(hidden)]const LAYOUT: Layout = _
sized_type_properties)Source§#[doc(hidden)]const MAX_SLICE_LEN: usize = _
#[doc(hidden)]const MAX_SLICE_LEN: usize = _
sized_type_properties)[Self]. Read more