pub unsafe trait MemPod: Copy + 'static {
// Provided methods
fn zeroed() -> Self { ... }
fn from_bytes(bytes: &[u8]) -> Self { ... }
fn as_bytes(&self) -> &[u8] ⓘ { ... }
fn as_bytes_mut(&mut self) -> &mut [u8] ⓘ { ... }
}
unsafe_layout
only.Expand description
Indicates a type is Plain Old Data, and meets specific memory layout guarantees.
Types that implement this trait are guaranteed to be safe for certain low-level memory operations, such as transmuting to and from byte slices, copying, and interfacing with C code.
§Safety
Implementing MemPod
is unsafe
because it requires the implementer to ensure the
following invariants:
-
No Padding: The type must not contain any padding bytes. This ensures that the memory representation of the type is fully defined by its fields.
-
Safe to Transmute: The type must be safe to transmute to and from a byte slice (
&[u8]
). This requires that the type’s memory layout is consistent and well-defined across different platforms. -
Copyable: The type must implement
Copy
, meaning it can be duplicated simply by copying its bits. This is a fundamental property of POD types. -
Valid Bit Patterns: Any bit pattern must represent a valid instance of the type. This means that transmuting arbitrary bytes into the type must always produce a valid value, and no bit pattern can cause undefined behavior when interpreted as this type.
§Implementing MemPod
When implementing MemPod
, it is recommended to use #[repr(C)]
or #[repr(transparent)]
on the type to ensure a well-defined and predictable memory layout. This is particularly
important when dealing with FFI or any situation where the exact memory layout of the type
is critical.
Note that only types that are Copy
can implement MemPod
. This is because POD types
inherently need to be trivially copyable without any additional logic (e.g. no destructors).
§Use Cases
- FFI Compatibility:
MemPod
types can be safely passed between Rust and C, as they have a predictable memory layout. - Efficient Serialization:
MemPod
types can be directly serialized to or deserialized from a byte buffer, making them ideal for low-level data manipulation and storage. - Memory Safety: By ensuring that types implementing
MemPod
adhere to strict memory guarantees, you can safely perform low-level operations without risking undefined behavior.
§Examples
use devela::sys::mem::MemPod;
// Define a simple structure that can be safely used as POD.
#[derive(Copy, Clone)]
#[repr(C)] // Ensure a predictable memory layout compatible with C.
struct MyStruct {
pub a: u8,
pub b: i16,
pub c: u32,
}
unsafe impl MemPod for MyStruct {}
Provided Methods§
Sourcefn from_bytes(bytes: &[u8]) -> Self
fn from_bytes(bytes: &[u8]) -> Self
Returns a new instance constructed from the given bytes.
If the byte slice is too small, the remaining bytes will be 0.
Sourcefn as_bytes_mut(&mut self) -> &mut [u8] ⓘ
fn as_bytes_mut(&mut self) -> &mut [u8] ⓘ
Returns the instance’s data as a mutable slice of bytes.
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§
impl MemPod for f32
impl MemPod for f64
impl MemPod for i8
impl MemPod for i16
impl MemPod for i32
impl MemPod for i64
impl MemPod for i128
impl MemPod for isize
impl MemPod for u8
impl MemPod for u16
impl MemPod for u32
impl MemPod for u64
impl MemPod for u128
impl MemPod for ()
impl MemPod for usize
impl<T: MemPod, const N: usize> MemPod for [T; N]
Implementors§
impl MemPod for Option<NonZeroI8>
impl MemPod for Option<NonZeroI16>
impl MemPod for Option<NonZeroI32>
impl MemPod for Option<NonZeroI64>
impl MemPod for Option<NonZeroI128>
impl MemPod for Option<NonZeroIsize>
impl MemPod for Option<NonZeroU8>
impl MemPod for Option<NonZeroU16>
impl MemPod for Option<NonZeroU32>
impl MemPod for Option<NonZeroU64>
impl MemPod for Option<NonZeroU128>
impl MemPod for Option<NonZeroUsize>
impl MemPod for char8
_char8
only.