MaybeDynSized

Trait MaybeDynSized 

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

Source

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§

Source

type Header: Header

The associated Header of this tag.

Required Methods§

Source

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§

Source

fn header(&self) -> &Self::Header

Returns the corresponding Header.

Source

fn payload(&self) -> &[u8]

Returns the payload, i.e., all memory that is not occupied by the Header of the type.

Source

fn as_bytes(&self) -> BytesRef<'_, Self::Header>

Returns the whole allocated bytes for this structure encapsulated in BytesRef. This includes padding bytes. To only get the “true” tag data, read the tag size from Self::header and create a sub slice.

Examples found in repository?
examples/minimal.rs (line 31)
9fn main() {
10    // We create a Multiboot2 header during runtime here. A more practical
11    // example, however, would be that you parse the header from kernel binary
12    // at runtime.
13    let mb2_hdr_bytes = Builder::new(HeaderTagISA::I386)
14        .relocatable_tag(RelocatableHeaderTag::new(
15            HeaderTagFlag::Required,
16            0x1337,
17            0xdeadbeef,
18            4096,
19            RelocatableHeaderTagPreference::None,
20        ))
21        .information_request_tag(InformationRequestHeaderTag::new(
22            HeaderTagFlag::Required,
23            &[
24                MbiTagType::Cmdline.into(),
25                MbiTagType::BootLoaderName.into(),
26            ],
27        ))
28        .build();
29
30    // Cast bytes in vector to Multiboot2 information structure
31    let ptr = mb2_hdr_bytes.as_bytes().as_ptr();
32    let mb2_hdr = unsafe { Multiboot2Header::load(ptr.cast()) };
33    let mb2_hdr = mb2_hdr.unwrap();
34    println!("{mb2_hdr:#?}");
35}
Source

fn as_ptr(&self) -> *const Self::Header

Returns a pointer to this structure.

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 MaybeDynSized for ApmTag

Source§

impl MaybeDynSized for BootLoaderNameTag

Source§

impl MaybeDynSized for BootdevTag

Source§

impl MaybeDynSized for CommandLineTag

Source§

impl MaybeDynSized for EFIBootServicesNotExitedTag

Source§

impl MaybeDynSized for EFIImageHandle32Tag

Source§

impl MaybeDynSized for EFIImageHandle64Tag

Source§

impl MaybeDynSized for EFISdt32Tag

Source§

impl MaybeDynSized for EFISdt64Tag

Source§

impl MaybeDynSized for ElfSectionsTag

Source§

impl MaybeDynSized for EndTag

Source§

impl MaybeDynSized for FramebufferTag

Source§

impl MaybeDynSized for ImageLoadPhysAddrTag

Source§

impl MaybeDynSized for BasicMemoryInfoTag

Source§

impl MaybeDynSized for EFIMemoryMapTag

Source§

impl MaybeDynSized for MemoryMapTag

Source§

impl MaybeDynSized for ModuleTag

Source§

impl MaybeDynSized for NetworkTag

Source§

impl MaybeDynSized for RsdpV1Tag

Source§

impl MaybeDynSized for RsdpV2Tag

Source§

impl MaybeDynSized for SmbiosTag

Source§

impl MaybeDynSized for VBEInfoTag

Implementors§

Source§

impl MaybeDynSized for DummyDstTag

Source§

impl MaybeDynSized for AddressHeaderTag

Source§

impl MaybeDynSized for ConsoleHeaderTag

Source§

impl MaybeDynSized for EfiBootServiceHeaderTag

Source§

impl MaybeDynSized for EndHeaderTag

Source§

impl MaybeDynSized for EntryAddressHeaderTag

Source§

impl MaybeDynSized for EntryEfi32HeaderTag

Source§

impl MaybeDynSized for EntryEfi64HeaderTag

Source§

impl MaybeDynSized for FramebufferHeaderTag

Source§

impl MaybeDynSized for InformationRequestHeaderTag

Source§

impl MaybeDynSized for ModuleAlignHeaderTag

Source§

impl MaybeDynSized for RelocatableHeaderTag

Source§

impl<H> MaybeDynSized for DynSizedStructure<H>
where H: Header,