ab_contracts_io_type::trivial_type

Trait TrivialType

Source
pub unsafe trait TrivialType
where 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§

Source

const METADATA: &[u8]

Data structure metadata in binary form, describing shape and types of the contents, see IoTypeMetadataKind for encoding details.

Provided Associated Constants§

Source

const SIZE: u32 = _

Provided Methods§

Source

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.

Source

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.

Source

fn as_bytes(&self) -> &[u8]

Access underlying byte representation of a data structure

Source

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.

Implementations on Foreign Types§

Source§

impl TrivialType for bool

Source§

impl TrivialType for i8

Source§

impl TrivialType for i16

Source§

impl TrivialType for i32

Source§

impl TrivialType for i64

Source§

impl TrivialType for i128

Source§

impl TrivialType for u8

Source§

impl TrivialType for u16

Source§

impl TrivialType for u32

Source§

impl TrivialType for u64

Source§

impl TrivialType for u128

Source§

impl TrivialType for ()

Source§

impl<const SIZE: usize, T> TrivialType for [T; SIZE]
where T: TrivialType,

Implementors§