Trait multiboot2::TagTrait
source · pub trait TagTrait: Pointee {
const ID: TagType;
// Required method
fn dst_size(base_tag: &Tag) -> Self::Metadata;
// Provided methods
fn as_base_tag(&self) -> &Tag { ... }
fn size(&self) -> usize { ... }
fn as_bytes(&self) -> &[u8] { ... }
unsafe fn from_base_tag<'a>(tag: &Tag) -> &'a Self { ... }
}
Expand description
A trait to abstract over all sized and unsized tags (DSTs). For sized tags,
this trait does not much. For DSTs, a TagTrait::dst_size
implementation
must be provided, which returns the right size hint for the dynamically
sized portion of the struct.
§Trivia
This crate uses the Pointee
-abstraction of the ptr_meta
crate to
create fat pointers for tags that are DST.
Required Associated Constants§
Required Methods§
sourcefn dst_size(base_tag: &Tag) -> Self::Metadata
fn dst_size(base_tag: &Tag) -> 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 as_base_tag(&self) -> &Tag
fn as_base_tag(&self) -> &Tag
Returns the tag as the common base tag structure.
sourcefn size(&self) -> usize
fn size(&self) -> usize
Returns the total size of the tag. The depends on the size
field of
the tag.
sourcefn as_bytes(&self) -> &[u8]
fn as_bytes(&self) -> &[u8]
Returns a slice to the underlying bytes of the tag. This includes all
bytes, also for tags that are DSTs. The slice length depends on the
size
field of the tag.
sourceunsafe fn from_base_tag<'a>(tag: &Tag) -> &'a Self
unsafe fn from_base_tag<'a>(tag: &Tag) -> &'a Self
Creates a reference to a (dynamically sized) tag type in a safe way.
DST tags need to implement a proper Self::dst_size
implementation.
§Safety
Callers must be sure that the “size” field of the provided Tag
is
sane and the underlying memory valid. The implementation of this trait
must have a correct Self::dst_size
implementation.