pub unsafe trait TrivialTypewhere
Self: Copy + 'static,{
const METADATA: &[u8];
const SIZE: u32 = _;
// Provided methods
unsafe fn read_unaligned(bytes: &[u8]) -> Option<Self> { ... }
unsafe fn read_unaligned_unchecked(bytes: &[u8]) -> Self { ... }
unsafe fn from_bytes(bytes: &[u8]) -> Option<&Self> { ... }
unsafe fn from_bytes_unchecked(bytes: &[u8]) -> &Self { ... }
unsafe fn from_bytes_mut(bytes: &mut [u8]) -> Option<&mut Self> { ... }
unsafe fn from_bytes_mut_unchecked(bytes: &mut [u8]) -> &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 layout that can be treated as bytes. It must not be relied on with untrusted data (it can be constructed from bytes, and internal invariants might not be invalid). Serializing and deserializing of types that implement this trait is simply casting of underlying memory. As a 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 read_unaligned(bytes: &[u8]) -> Option<Self>
unsafe fn read_unaligned(bytes: &[u8]) -> Option<Self>
Read unaligned value from memory.
Returns None if the number of input bytes is not sufficient to represent the type.
§Safety
Input bytes must be previously produced by taking underlying bytes of the same type, or else the data structure might have unexpected contents. While technically not unsafe, this API should be used very carefully and data structure invariants need to be checked manually.
Sourceunsafe fn read_unaligned_unchecked(bytes: &[u8]) -> Self
unsafe fn read_unaligned_unchecked(bytes: &[u8]) -> Self
Similar to Self::read_unaligned(), but doesn’t do any checks at all.
§Safety
The number of bytes must be at least as big as the type itself.
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. Size must be sufficient,
padding beyond the size of the type is allowed.
§Safety
Input bytes must be previously produced by taking underlying bytes of the same type, or else the data structure might have unexpected contents. While technically not unsafe, this API should be used very carefully and data structure invariants need to be checked manually.
Sourceunsafe fn from_bytes_unchecked(bytes: &[u8]) -> &Self
unsafe fn from_bytes_unchecked(bytes: &[u8]) -> &Self
Similar to Self::from_bytes(), but doesn’t do any checks at all.
§Safety
Size is at least as big as the type itself, and alignment is correct.
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. Size must be sufficient,
padding beyond the size of the type is allowed.
§Safety
Input bytes must be previously produced by taking underlying bytes of the same type, or else the data structure might have unexpected contents. While technically not unsafe, this API should be used very carefully and data structure invariants need to be checked manually.
Sourceunsafe fn from_bytes_mut_unchecked(bytes: &mut [u8]) -> &mut Self
unsafe fn from_bytes_mut_unchecked(bytes: &mut [u8]) -> &mut Self
Similar to Self::from_bytes_mut(), but doesn’t do any checks at all.
§Safety
Size is at least as big as the type itself, and alignment is correct.
Sourcefn as_bytes(&self) -> &[u8; { _ }]
fn as_bytes(&self) -> &[u8; { _ }]
Access the underlying byte representation of a data structure
Sourceunsafe fn as_bytes_mut(&mut self) -> &mut [u8; { _ }]
unsafe fn as_bytes_mut(&mut self) -> &mut [u8; { _ }]
Access the 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.