Trait KnownLayout

Source
pub unsafe trait KnownLayout {
    type PointerMetadata: PointerMetadata;
}
Expand description

Indicates that zerocopy can reason about certain aspects of a type’s layout.

This trait is required by many of zerocopy’s APIs. It supports sized types, slices, and slice DSTs.

§Implementation

Do not implement this trait yourself! Instead, use #[derive(KnownLayout)]; e.g.:

#[derive(KnownLayout)]
struct MyStruct {
    ...
}

#[derive(KnownLayout)]
enum MyEnum {
    ...
}

#[derive(KnownLayout)]
union MyUnion {
    ...
}

This derive performs a sophisticated analysis to deduce the layout characteristics of types. You must implement this trait via the derive.

§Dynamically-sized types

KnownLayout supports slice-based dynamically sized types (“slice DSTs”).

A slice DST is a type whose trailing field is either a slice or another slice DST, rather than a type with fixed size. For example:

#[repr(C)]
struct PacketHeader {
    ...
}

#[repr(C)]
struct Packet {
    header: PacketHeader,
    body: [u8],
}

It can be useful to think of slice DSTs as a generalization of slices - in other words, a normal slice is just the special case of a slice DST with zero leading fields. In particular:

  • Like slices, slice DSTs can have different lengths at runtime
  • Like slices, slice DSTs cannot be passed by-value, but only by reference or via other indirection such as Box
  • Like slices, a reference (or Box, or other pointer type) to a slice DST encodes the number of elements in the trailing slice field

§Slice DST layout

Just like other composite Rust types, the layout of a slice DST is not well-defined unless it is specified using an explicit #[repr(...)] attribute such as #[repr(C)]. Other representations are supported, but in this section, we’ll use #[repr(C)] as our example.

A #[repr(C)] slice DST is laid out just like sized #[repr(C)] types, but the presenence of a variable-length field introduces the possibility of dynamic padding. In particular, it may be necessary to add trailing padding after the trailing slice field in order to satisfy the outer type’s alignment, and the amount of padding required may be a function of the length of the trailing slice field. This is just a natural consequence of the normal #[repr(C)] rules applied to slice DSTs, but it can result in surprising behavior. For example, consider the following type:

#[repr(C)]
struct Foo {
    a: u32,
    b: u8,
    z: [u16],
}

Assuming that u32 has alignment 4 (this is not true on all platforms), then Foo has alignment 4 as well. Here is the smallest possible value for Foo:

byte offset | 01234567
      field | aaaab---
                   ><

In this value, z has length 0. Abiding by #[repr(C)], the lowest offset that we can place z at is 5, but since z has alignment 2, we need to round up to offset 6. This means that there is one byte of padding between b and z, then 0 bytes of z itself (denoted >< in this diagram), and then two bytes of padding after z in order to satisfy the overall alignment of Foo. The size of this instance is 8 bytes.

What about if z has length 1?

byte offset | 01234567
      field | aaaab-zz

In this instance, z has length 1, and thus takes up 2 bytes. That means that we no longer need padding after z in order to satisfy Foo’s alignment. We’ve now seen two different values of Foo with two different lengths of z, but they both have the same size - 8 bytes.

What about if z has length 2?

byte offset | 012345678901
      field | aaaab-zzzz--

Now z has length 2, and thus takes up 4 bytes. This brings our un-padded size to 10, and so we now need another 2 bytes of padding after z to satisfy Foo’s alignment.

Again, all of this is just a logical consequence of the #[repr(C)] rules applied to slice DSTs, but it can be surprising that the amount of trailing padding becomes a function of the trailing slice field’s length, and thus can only be computed at runtime.

§What is a valid size?

There are two places in zerocopy’s API that we refer to “a valid size” of a type. In normal casts or conversions, where the source is a byte slice, we need to know whether the source byte slice is a valid size of the destination type. In prefix or suffix casts, we need to know whether there exists a valid size of the destination type which fits in the source byte slice and, if so, what the largest such size is.

As outlined above, a slice DST’s size is defined by the number of elements in its trailing slice field. However, there is not necessarily a 1-to-1 mapping between trailing slice field length and overall size. As we saw in the previous section with the type Foo, instances with both 0 and 1 elements in the trailing z field result in a Foo whose size is 8 bytes.

When we say “x is a valid size of T”, we mean one of two things:

  • If T: Sized, then we mean that x == size_of::<T>()
  • If T is a slice DST, then we mean that there exists a len such that the instance of T with len trailing slice elements has size x

When we say “largest possible size of T that fits in a byte slice”, we mean one of two things:

  • If T: Sized, then we mean size_of::<T>() if the byte slice is at least size_of::<T>() bytes long
  • If T is a slice DST, then we mean to consider all values, len, such that the instance of T with len trailing slice elements fits in the byte slice, and to choose the largest such len, if any

§Safety

This trait does not convey any safety guarantees to code outside this crate.

You must not rely on the #[doc(hidden)] internals of KnownLayout. Future releases of zerocopy may make backwards-breaking changes to these items, including changes that only affect soundness, which may cause code which uses those items to silently become unsound.

Required Associated Types§

Source

type PointerMetadata: PointerMetadata

The type of metadata stored in a pointer to Self.

This is () for sized types and usize for slice DSTs.

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.

Implementations on Foreign Types§

Source§

impl KnownLayout for bool

Source§

impl KnownLayout for char

Source§

impl KnownLayout for f32

Source§

impl KnownLayout for f64

Source§

impl KnownLayout for i8

Source§

impl KnownLayout for i16

Source§

impl KnownLayout for i32

Source§

impl KnownLayout for i64

Source§

impl KnownLayout for i128

Source§

impl KnownLayout for isize

Source§

impl KnownLayout for str

Source§

impl KnownLayout for u8

Source§

impl KnownLayout for u16

Source§

impl KnownLayout for u32

Source§

impl KnownLayout for u64

Source§

impl KnownLayout for u128

Source§

impl KnownLayout for ()

Source§

impl KnownLayout for usize

Source§

impl KnownLayout for __m128

Source§

impl KnownLayout for __m128d

Source§

impl KnownLayout for __m128i

Source§

impl KnownLayout for __m256

Source§

impl KnownLayout for __m256d

Source§

impl KnownLayout for __m256i

Source§

impl KnownLayout for AtomicBool

Source§

impl KnownLayout for AtomicI8

Source§

impl KnownLayout for AtomicI16

Source§

impl KnownLayout for AtomicI32

Source§

impl KnownLayout for AtomicI64

Source§

impl KnownLayout for AtomicIsize

Source§

impl KnownLayout for AtomicU8

Source§

impl KnownLayout for AtomicU16

Source§

impl KnownLayout for AtomicU32

Source§

impl KnownLayout for AtomicU64

Source§

impl KnownLayout for AtomicUsize

Source§

impl KnownLayout for NonZeroI8

Source§

impl KnownLayout for NonZeroI16

Source§

impl KnownLayout for NonZeroI32

Source§

impl KnownLayout for NonZeroI64

Source§

impl KnownLayout for NonZeroI128

Source§

impl KnownLayout for NonZeroIsize

Source§

impl KnownLayout for NonZeroU8

Source§

impl KnownLayout for NonZeroU16

Source§

impl KnownLayout for NonZeroU32

Source§

impl KnownLayout for NonZeroU64

Source§

impl KnownLayout for NonZeroU128

Source§

impl KnownLayout for NonZeroUsize

Source§

impl<T> KnownLayout for Option<T>

Source§

impl<T> KnownLayout for [T]

Source§

impl<T> KnownLayout for Wrapping<T>

Source§

impl<T> KnownLayout for AtomicPtr<T>

Source§

impl<T> KnownLayout for CoreMaybeUninit<T>

Source§

impl<T, const N: usize> KnownLayout for [T; N]

Source§

impl<T: ?Sized + KnownLayout> KnownLayout for Cell<T>

Source§

impl<T: ?Sized + KnownLayout> KnownLayout for UnsafeCell<T>

Source§

impl<T: ?Sized + KnownLayout> KnownLayout for ManuallyDrop<T>

Source§

impl<T: ?Sized> KnownLayout for *const T

Source§

impl<T: ?Sized> KnownLayout for *mut T

Source§

impl<T: ?Sized> KnownLayout for &T

Source§

impl<T: ?Sized> KnownLayout for &mut T

Source§

impl<T: ?Sized> KnownLayout for PhantomData<T>

Implementors§

Source§

impl<O> KnownLayout for F32<O>
where Self: Sized,

Source§

impl<O> KnownLayout for F64<O>
where Self: Sized,

Source§

impl<O> KnownLayout for I16<O>
where Self: Sized,

Source§

impl<O> KnownLayout for I32<O>
where Self: Sized,

Source§

impl<O> KnownLayout for I64<O>
where Self: Sized,

Source§

impl<O> KnownLayout for I128<O>
where Self: Sized,

Source§

impl<O> KnownLayout for Isize<O>
where Self: Sized,

Source§

impl<O> KnownLayout for U16<O>
where Self: Sized,

Source§

impl<O> KnownLayout for U32<O>
where Self: Sized,

Source§

impl<O> KnownLayout for U64<O>
where Self: Sized,

Source§

impl<O> KnownLayout for U128<O>
where Self: Sized,

Source§

impl<O> KnownLayout for Usize<O>
where Self: Sized,

Source§

impl<T> KnownLayout for Unalign<T>

impl KnownLayout for BlockFifoCommand
where u32: KnownLayout,

impl KnownLayout for BlockFifoRequest
where u64: KnownLayout,

impl KnownLayout for BlockFifoResponse
where [u64; 4]: KnownLayout,

impl KnownLayout for Header
where Self: Sized,

impl KnownLayout for EapolFields
where BigEndianU16: KnownLayout,

impl KnownLayout for KeyDescriptor
where u8: KnownLayout,

impl KnownLayout for KeyFrameFields
where [u8; 8]: KnownLayout,

impl KnownLayout for KeyInformation
where u16: KnownLayout,

impl KnownLayout for PacketType
where u8: KnownLayout,

impl KnownLayout for ProtocolVersion
where u8: KnownLayout,

impl KnownLayout for BlockGroupDesc32
where LEU16: KnownLayout,

impl KnownLayout for DirEntryHeader
where u8: KnownLayout,

impl KnownLayout for Extent
where LEU32: KnownLayout,

impl KnownLayout for ExtentHeader
where LEU32: KnownLayout,

impl KnownLayout for ExtentIndex
where LEU16: KnownLayout,

impl KnownLayout for INode
where LEU32: KnownLayout,

impl KnownLayout for SuperBlock
where LEU32: KnownLayout,

impl KnownLayout for XattrEntryHeader
where LEU32: KnownLayout,

impl KnownLayout for XattrHeader
where [u8; 8]: KnownLayout,

impl<B> KnownLayout for ExtentTreeNode<B>
where B: KnownLayout + SplitByteSlice,

impl KnownLayout for zbi_bootfs_dirent_t
where U32: KnownLayout,

impl KnownLayout for zbi_bootfs_header_t
where U32: KnownLayout,

impl KnownLayout for ZbiTopologyArm64Info
where u8: KnownLayout,

impl KnownLayout for ZbiTopologyCache
where u32: KnownLayout,

impl KnownLayout for ZbiTopologyCluster
where u8: KnownLayout,

impl KnownLayout for ZbiTopologyNode
where Entity: KnownLayout,

impl KnownLayout for ZbiTopologyNumaRegion
where u64: KnownLayout,

impl KnownLayout for ZbiTopologyProcessor
where ArchitectureInfo: KnownLayout,

impl KnownLayout for ZbiTopologyX64Info
where u32: KnownLayout,

impl KnownLayout for zbi_header_t
where U32: KnownLayout,

impl KnownLayout for ArchitectureInfo
where Self: Sized,

impl KnownLayout for Entity
where Self: Sized,

impl KnownLayout for ProxyFilename
where [u8; 32]: KnownLayout,

impl KnownLayout for Header
where u32: KnownLayout,

impl KnownLayout for PartitionTableEntry
where [u16; 36]: KnownLayout,

impl KnownLayout for Bssid
where Self: Sized,

impl KnownLayout for MacAddr
where Self: Sized,

impl KnownLayout for Type00Config
where u8: KnownLayout,

impl KnownLayout for Type01Config
where u16: KnownLayout,

impl KnownLayout for A
where Self: Sized,

impl KnownLayout for Aaaa
where Self: Sized,

impl KnownLayout for Header
where U16: KnownLayout,

impl KnownLayout for Mac
where Self: Sized,

impl KnownLayout for Ipv4Addr
where Self: Sized,

impl KnownLayout for Ipv6Addr
where Self: Sized,

impl KnownLayout for PacketHead
where U32: KnownLayout,

impl KnownLayout for Mldv1Message
where Ipv6Addr: KnownLayout,

impl KnownLayout for Mldv2QueryMessageHeader
where U16: KnownLayout,

impl KnownLayout for Mldv2ReportHeader
where U16: KnownLayout,

impl KnownLayout for Mldv2ReportRecordHeader
where Ipv6Addr: KnownLayout,

impl KnownLayout for MulticastListenerDone
where Self: Sized,

impl KnownLayout for MulticastListenerQuery
where Self: Sized,

impl KnownLayout for MulticastListenerQueryV2
where Self: Sized,

impl KnownLayout for MulticastListenerReport
where Self: Sized,

impl KnownLayout for MulticastListenerReportV2
where Self: Sized,

impl KnownLayout for PrefixInformation
where Ipv6Addr: KnownLayout,

impl KnownLayout for NeighborAdvertisement
where Ipv6Addr: KnownLayout,

impl KnownLayout for NeighborSolicitation
where Ipv6Addr: KnownLayout,

impl KnownLayout for Redirect
where Ipv6Addr: KnownLayout,

impl KnownLayout for RouterAdvertisement
where U32: KnownLayout,

impl KnownLayout for RouterSolicitation
where [u8; 4]: KnownLayout,

impl KnownLayout for IcmpDestUnreachable
where U16: KnownLayout,

impl KnownLayout for IcmpEchoReply
where IdAndSeq: KnownLayout,

impl KnownLayout for IcmpEchoRequest
where IdAndSeq: KnownLayout,

impl KnownLayout for IcmpTimeExceeded
where [u8; 4]: KnownLayout,

impl KnownLayout for Icmpv4ParameterProblem
where [u8; 3]: KnownLayout,

impl KnownLayout for Icmpv4Redirect
where Ipv4Addr: KnownLayout,

impl KnownLayout for Icmpv4TimestampReply
where Self: Sized,

impl KnownLayout for Icmpv4TimestampRequest
where Self: Sized,

impl KnownLayout for Icmpv6PacketTooBig
where U32: KnownLayout,

impl KnownLayout for Icmpv6ParameterProblem
where U32: KnownLayout,

impl KnownLayout for GroupRecordHeader
where Ipv4Addr: KnownLayout,

impl KnownLayout for MembershipQueryData
where U16: KnownLayout,

impl KnownLayout for MembershipReportV3Data
where U16: KnownLayout,

impl KnownLayout for HeaderPrefix
where [u8; 2]: KnownLayout,

impl KnownLayout for DscpAndEcn
where u8: KnownLayout,

impl KnownLayout for HeaderPrefix
where Ipv4Addr: KnownLayout,

impl KnownLayout for FixedHeader
where Ipv6Addr: KnownLayout,

impl KnownLayout for TcpSackBlock
where U32: KnownLayout,

impl KnownLayout for TcpFlowAndSeqNum
where U32: KnownLayout,

impl KnownLayout for TcpFlowHeader
where U16: KnownLayout,

impl KnownLayout for Elf32Dyn
where u32: KnownLayout,

impl KnownLayout for Elf32FileHeader
where u16: KnownLayout,

impl KnownLayout for Elf32ProgramHeader
where u32: KnownLayout,

impl KnownLayout for Elf64Dyn
where u64: KnownLayout,

impl KnownLayout for Elf64FileHeader
where u16: KnownLayout,

impl KnownLayout for Elf64ProgramHeader
where u64: KnownLayout,

impl KnownLayout for ElfIdent
where [u8; 7]: KnownLayout,

impl KnownLayout for elf32_sym
where Elf32Half: KnownLayout,

impl KnownLayout for elf64_sym
where Elf64Xword: KnownLayout,

impl KnownLayout for EUI48
where [u8; 6]: KnownLayout,

impl KnownLayout for EUI64
where [u8; 8]: KnownLayout,

impl KnownLayout for PacketType
where Self: Sized,

impl KnownLayout for Header
where U32: KnownLayout,

impl KnownLayout for virtgralloc_set_vulkan_mode

impl KnownLayout for BigEndianU128
where [u8; 16]: KnownLayout,

impl KnownLayout for BigEndianU16
where [u8; 2]: KnownLayout,

impl KnownLayout for BigEndianU32
where [u8; 4]: KnownLayout,

impl KnownLayout for BigEndianU64
where [u8; 8]: KnownLayout,

impl KnownLayout for AmpduParams
where u8: KnownLayout,

impl KnownLayout for ApWmmInfo
where u8: KnownLayout,

impl KnownLayout for AselCapability
where u8: KnownLayout,

impl KnownLayout for BitmapControl
where u8: KnownLayout,

impl KnownLayout for BssMaxIdlePeriod
where IdleOptions: KnownLayout,

impl KnownLayout for ChannelSwitchAnnouncement
where u8: KnownLayout,

impl KnownLayout for ClientWmmInfo
where u8: KnownLayout,

impl KnownLayout for DsssParamSet
where u8: KnownLayout,

impl KnownLayout for EcwMinMax
where u8: KnownLayout,

impl KnownLayout for ExtCapabilitiesOctet1
where u8: KnownLayout,

impl KnownLayout for ExtCapabilitiesOctet2
where u8: KnownLayout,

impl KnownLayout for ExtCapabilitiesOctet3
where u8: KnownLayout,

impl KnownLayout for ExtendedChannelSwitchAnnouncement
where u8: KnownLayout,

impl KnownLayout for Header
where u8: KnownLayout,

impl KnownLayout for HtCapabilities
where AselCapability: KnownLayout,

impl KnownLayout for HtCapabilityInfo
where u16: KnownLayout,

impl KnownLayout for HtExtCapabilities
where u16: KnownLayout,

impl KnownLayout for HtOpInfo
where [u8; 5]: KnownLayout,

impl KnownLayout for HtOperation
where SupportedMcsSet: KnownLayout,

impl KnownLayout for Id
where u8: KnownLayout,

impl KnownLayout for IdleOptions
where u8: KnownLayout,

impl KnownLayout for MpmProtocol
where u16: KnownLayout,

impl KnownLayout for PerrDestinationFlags
where u8: KnownLayout,

impl KnownLayout for PerrDestinationHeader
where u32: KnownLayout,

impl KnownLayout for PerrHeader
where u8: KnownLayout,

impl KnownLayout for PrepFlags
where u8: KnownLayout,

impl KnownLayout for PrepHeader
where u32: KnownLayout,

impl KnownLayout for PrepTail
where u32: KnownLayout,

impl KnownLayout for PreqFlags
where u8: KnownLayout,

impl KnownLayout for PreqHeader
where u32: KnownLayout,

impl KnownLayout for PreqMiddle
where u8: KnownLayout,

impl KnownLayout for PreqPerTarget
where u32: KnownLayout,

impl KnownLayout for PreqPerTargetFlags
where u8: KnownLayout,

impl KnownLayout for RmEnabledCapabilities
where [u8; 5]: KnownLayout,

impl KnownLayout for SecChanOffset
where u8: KnownLayout,

impl KnownLayout for SupportedMcsSet
where u128: KnownLayout,

impl KnownLayout for SupportedRate
where u8: KnownLayout,

impl KnownLayout for TimHeader
where BitmapControl: KnownLayout,

impl KnownLayout for TransmitPower
where u8: KnownLayout,

impl KnownLayout for TransmitPowerInfo
where u8: KnownLayout,

impl KnownLayout for TxBfCapability
where u32: KnownLayout,

impl KnownLayout for VhtCapabilities
where VhtMcsNssSet: KnownLayout,

impl KnownLayout for VhtCapabilitiesInfo
where u32: KnownLayout,

impl KnownLayout for VhtChannelBandwidth
where u8: KnownLayout,

impl KnownLayout for VhtMcsNssMap
where u16: KnownLayout,

impl KnownLayout for VhtMcsNssSet
where u64: KnownLayout,

impl KnownLayout for VhtOperation
where VhtMcsNssMap: KnownLayout,

impl KnownLayout for WideBandwidthChannelSwitch
where u8: KnownLayout,

impl KnownLayout for WmmAcParams
where u16: KnownLayout,

impl KnownLayout for WmmAciAifsn
where u8: KnownLayout,

impl KnownLayout for WmmInfo
where u8: KnownLayout,

impl KnownLayout for WmmParam
where WmmAcParams: KnownLayout,

impl KnownLayout for AttributeHeader
where BigEndianU16: KnownLayout,

impl KnownLayout for Id
where [u8; 2]: KnownLayout,

impl KnownLayout for WpsState
where u8: KnownLayout,

impl KnownLayout for ActionCategory
where u8: KnownLayout,

impl KnownLayout for ActionHdr
where ActionCategory: KnownLayout,

impl KnownLayout for AddbaReqHdr

impl KnownLayout for AddbaRespHdr
where u16: KnownLayout,

impl KnownLayout for AmsduSubframeHdr
where BigEndianU16: KnownLayout,

impl KnownLayout for AssocReqHdr
where u16: KnownLayout,

impl KnownLayout for AssocRespHdr
where Aid: KnownLayout,

impl KnownLayout for AuthAlgorithmNumber
where u16: KnownLayout,

impl KnownLayout for AuthHdr
where StatusCode: KnownLayout,

impl KnownLayout for BeaconHdr
where CapabilityInfo: KnownLayout,

impl KnownLayout for BlockAckAction
where u8: KnownLayout,

impl KnownLayout for BlockAckParameters
where u16: KnownLayout,

impl KnownLayout for BlockAckPolicy
where u8: KnownLayout,

impl KnownLayout for BlockAckStartingSequenceControl
where u16: KnownLayout,

impl KnownLayout for CapabilityInfo
where u16: KnownLayout,

impl KnownLayout for DeauthHdr
where ReasonCode: KnownLayout,

impl KnownLayout for DelbaHdr
where ReasonCode: KnownLayout,

impl KnownLayout for DelbaParameters
where u16: KnownLayout,

impl KnownLayout for DisassocHdr
where ReasonCode: KnownLayout,

impl KnownLayout for EthernetIIHdr
where BigEndianU16: KnownLayout,

impl KnownLayout for FixedDataHdrFields
where SequenceControl: KnownLayout,

impl KnownLayout for FrameControl
where u16: KnownLayout,

impl KnownLayout for HtControl
where u32: KnownLayout,

impl KnownLayout for LlcHdr
where BigEndianU16: KnownLayout,

impl KnownLayout for MgmtHdr
where SequenceControl: KnownLayout,

impl KnownLayout for ProbeRespHdr
where CapabilityInfo: KnownLayout,

impl KnownLayout for PsPoll
where MacAddr: KnownLayout,

impl KnownLayout for QosControl
where u16: KnownLayout,

impl KnownLayout for ReasonCode
where u16: KnownLayout,

impl KnownLayout for SequenceControl
where u16: KnownLayout,

impl KnownLayout for SpectrumMgmtAction
where u8: KnownLayout,

impl KnownLayout for StatusCode
where u16: KnownLayout,

impl KnownLayout for Oui
where [u8; 3]: KnownLayout,

impl KnownLayout for TimeUnit
where u16: KnownLayout,

impl KnownLayout for Koid
where Self: Sized,

impl KnownLayout for Name
where Self: Sized,

impl KnownLayout for zx_exception_context_t
where u32: KnownLayout,

impl KnownLayout for zx_exception_header_t
where zx_excp_type_t: KnownLayout,

impl KnownLayout for zx_exception_report_t
where zx_exception_context_t: KnownLayout,

impl KnownLayout for zx_info_socket_t
where usize: KnownLayout,

impl KnownLayout for zx_info_vmo_t
where u64: KnownLayout,

impl KnownLayout for zx_sched_deadline_params_t
where zx_duration_t: KnownLayout,

impl KnownLayout for zx_thread_state_general_regs_t
where u64: KnownLayout,

impl KnownLayout for zx_x86_64_exc_data_t
where u64: KnownLayout,

impl KnownLayout for InfoMapsTypeUnion
where Self: Sized,

impl KnownLayout for zx_exception_header_arch_t
where Self: Sized,