pub trait MaybeDynSized: Pointee {
type Header: Header;
const BASE_SIZE: usize;
// Required method
fn dst_len(header: &Self::Header) -> Self::Metadata;
// Provided methods
fn header(&self) -> &Self::Header { ... }
fn payload(&self) -> &[u8] { ... }
fn as_bytes(&self) -> BytesRef<'_, Self::Header> { ... }
fn as_ptr(&self) -> *const Self::Header { ... }
}
Expand description
A trait to abstract sized and unsized structures (DSTs). It enables
casting a DynSizedStructure
to sized or unsized structures using
DynSizedStructure::cast
.
Structs that are a DST must provide a correct MaybeDynSized::dst_len
implementation.
§ABI
Implementors must use #[repr(C)]
. As there might be padding necessary
for the proper Rust layout, size_of_val(&self)
might report additional
padding bytes that are not reflected by the actual payload. These additional
padding bytes however will be reflected in corresponding BytesRef
instances.
Required Associated Constants§
Sourceconst BASE_SIZE: usize
const BASE_SIZE: usize
The true base size of the struct without any implicit or additional
padding. Note that size_of::<T>()
isn’t sufficient, as for example
the type could have three u32
fields, which would add an implicit
u32
padding. However, this constant must always fulfill
BASE_SIZE >= size_of::<Self::Header>()
.
The main purpose of this constant is to create awareness when you
implement Self::dst_len
, where you should use this. If this value
is correct, we prevent situations where we read uninitialized bytes,
especially when creating tags in builders.
Required Associated Types§
Required Methods§
Sourcefn dst_len(header: &Self::Header) -> Self::Metadata
fn dst_len(header: &Self::Header) -> Self::Metadata
Returns the amount of items in the dynamically sized portion of the DST. Note that this is not the amount of bytes. So if the dynamically sized portion is 16 bytes in size and each element is 4 bytes big, then this function must return 4.
For sized tags, this just returns ()
. For DSTs, this returns an
usize
.
Provided Methods§
Sourcefn payload(&self) -> &[u8]
fn payload(&self) -> &[u8]
Returns the payload, i.e., all memory that is not occupied by the
Header
of the type.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.