pub unsafe trait TrivialTypewhere
Self: Copy + 'static,{
const METADATA: &[u8];
const SIZE: u32 = _;
// Provided methods
unsafe fn from_bytes(bytes: &[u8]) -> Option<&Self> { ... }
unsafe fn from_bytes_mut(bytes: &mut [u8]) -> Option<&mut Self> { ... }
fn as_bytes(&self) -> &[u8] { ... }
unsafe fn as_bytes_mut(&mut self) -> &mut [u8] { ... }
}
Expand description
Simple wrapper data type that is designed in such a way that its serialization/deserialization is the same as the type itself.
§Safety
This trait is used for types with memory transmutation capabilities, it must not be relied on with untrusted data. Serializing and deserializing of types that implement this trait is simply casting of underlying memory, as the result all the types implementing this trait must not use implicit padding, unions or anything similar that might make it unsound to access any bits of the type.
Helper functions are provided to make casting to/from bytes a bit safer than it would otherwise, but extra care is still needed.
Do not implement this trait explicitly! Use #[derive(TrivialType)]
instead, which will
ensure safety requirements are upheld.
Required Associated Constants§
Sourceconst METADATA: &[u8]
const METADATA: &[u8]
Data structure metadata in binary form, describing shape and types of the contents, see
IoTypeMetadataKind
for encoding details.
Provided Associated Constants§
Provided Methods§
Sourceunsafe fn from_bytes(bytes: &[u8]) -> Option<&Self>
unsafe fn from_bytes(bytes: &[u8]) -> Option<&Self>
Create a reference to a type, which is represented by provided memory.
Memory must be correctly aligned or else None
will be returned, but padding beyond the
size of the type is allowed.
§Safety
Input bytes must be previously produced by taking underlying bytes of the same type.
Sourceunsafe fn from_bytes_mut(bytes: &mut [u8]) -> Option<&mut Self>
unsafe fn from_bytes_mut(bytes: &mut [u8]) -> Option<&mut Self>
Create a mutable reference to a type, which is represented by provided memory.
Memory must be correctly aligned or else None
will be returned, but padding beyond the
size of the type is allowed.
§Safety
Input bytes must be previously produced by taking underlying bytes of the same type.
Sourceunsafe fn as_bytes_mut(&mut self) -> &mut [u8]
unsafe fn as_bytes_mut(&mut self) -> &mut [u8]
Access underlying mutable byte representation of a data structure.
§Safety
While calling this function is technically safe, modifying returned memory buffer may result in broken invariants of underlying data structure and should be done with extra care.
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.