Skip to content

Commit 06b14e4

Browse files
committed
aaa
1 parent 82b93e7 commit 06b14e4

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

multiboot2-common/src/lib.rs

+21-9
Original file line numberDiff line numberDiff line change
@@ -120,17 +120,22 @@
120120
//!
121121
//! ## Rustc Requirements
122122
//!
123-
//! The allocation space that Rust requires for types is a multiple of the
123+
//! The required allocation space that Rust uses for types is a multiple of the
124124
//! alignment. This means that if we cast between byte slices and specific
125-
//! types, Rust doesn't just see the size reported by the header but also
126-
//! any necessary padding bytes. If this is not the case, for example we
127-
//! cast to a structure from a `&[u8; 15]`, Miri will complain as it expects
128-
//! `&[u8; 16]`
125+
//! types, Rust doesn't just see the "trimmed down actual payload" defined by
126+
//! struct members, but also any necessary, but hidden, padding bytes. If we
127+
//! don't guarantee the correct is not the case, for example we cast the bytes
128+
//! from a `&[u8; 15]` to an 8-byte aligned struct, Miri will complain as it
129+
//! expects `&[u8; 16]`.
129130
//!
130131
//! See <https://doc.rust-lang.org/reference/type-layout.html> for information.
131132
//!
133+
//! Further, this also means that we can't cast smaller structs to
134+
//!
132135
//! Further, the [`Layout`]
133136
//!
137+
//!
138+
//!
134139
//! # Provided Abstractions
135140
//!
136141
//! Figure 2 in the [README](https://crates.io/crates/multiboot2-common)
@@ -317,10 +322,17 @@ impl<H: Header> DynSizedStructure<H> {
317322
&self.payload
318323
}
319324

320-
/// Casts the structure tag to a specific [`MaybeDynSized`] implementation which
321-
/// may be a ZST or DST typed tag. The output type will have the exact same
322-
/// size as `*self`. The target type must be sufficient for that. If not,
323-
/// the function will panic.
325+
/// Performs a memory-safe same-size cast from the base-structure to a
326+
/// specific [`MaybeDynSized`]. The idea here is to cast the generic
327+
/// mostly semantic-free version to a specific type with fields that have
328+
/// a semantic.
329+
///
330+
/// The provided `T` of type [`MaybeDynSized`] might be may be sized type
331+
/// or DST. This depends on the type.
332+
///
333+
/// # Panic
334+
/// Panics if base assumptions are violated. For example, the
335+
/// `T` of type [`MaybeDynSized`] must allow a proper casting to it.
324336
///
325337
/// # Safety
326338
/// This function is safe due to various sanity checks and the overall

multiboot2-common/src/tag.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ use ptr_meta::Pointee;
1010
/// [`DynSizedStructure::cast`].
1111
///
1212
/// Structs that are a DST must provide a **correct**
13-
/// [`MaybeDynSized::dst_len`] implementation.
13+
/// [`MaybeDynSized::dst_len`] implementation. Further, implementors **must**
14+
/// use `#[repr(C)]`.
1415
///
1516
/// [`ID`]: Tag::ID
1617
/// [`DynSizedStructure`]: crate::DynSizedStructure

0 commit comments

Comments
 (0)