Skip to main content

ab_io_type/
lib.rs

1//! Infrastructure for zero-cost zero-copy serialization/deserialization.
2//!
3//! <div class="warning">
4//! This crate only supports little-endian platforms by design.
5//! </div>
6//!
7//! This crate primarily offers the following:
8//! * [`TrivialType`] trait
9//! * [`IoType`] trait
10//! * metadata describing types implementing the above traits (see [`IoTypeMetadataKind`])
11//!
12//! [`IoTypeMetadataKind`]: metadata::IoTypeMetadataKind
13//!
14//! ## `TrivialType`
15//! This trait is implemented for a bunch of built-in types and can be derived for custom types that
16//! contain them (structs and enums). It represents trivial types, which do not contain
17//! uninitialized bytes and are fully represented by their byte representation.
18//!
19//! What this means is that serialization to bytes can be done by simply casting a pointer to a data
20//! structure to an array of bytes. Similarly, deserialization of correctly aligned memory is simply
21//! casting of a pointer back to the data structure. The trait provides a few helper methods for
22//! dealing with serialization/deserialization.
23//!
24//! ## `IoType`
25//! This trait is implemented for all types that implement [`TrivialType`] and for a few additional
26//! custom types that have special properties and are useful for FFI purposes.
27//!
28//! `IoType` data structures can contain optional values or lists of values of a dynamic size. They
29//! are not as composable as `TrivialType` and are usually used as wrappers of the highest level.
30//! This is in contrast to `TrivialType` that is always fixed size and can't have optional data.
31//!
32//! ## Metadata
33//!
34//! The data structures implementing [`TrivialType`] and [`IoType`] traits have the `METADATA`
35//! associated constant (see [`IoTypeMetadataKind`]). This field contains a compact binary
36//! representation of the recursive layout of the type, which can then be decoded by the machine for
37//! FFI purposes or converted into human-readable format for presentation to the user in a somewhat
38//! readable way.
39//!
40//! The metadata contains both the memory layout and the names of data structures and fields.
41//! Metadata can also be compressed into an equivalent layout without field names to shrink the size
42//! of the metadata further when human readability is not necessary. Compressed metadata can also
43//! be hashed to get a "fingerprint" of the data structure, which may be used to distinguish a
44//! compatible FFI interface from an incompatible one based on the data layout, not just the number
45//! of bytes.
46//!
47//! ## Overall
48//!
49//! These traits are designed for zero-cost zero-copy serialization/deserialization. Any correctly
50//! aligned memory (both normal and memory-mapped files with `mmap`) can be interpreted as
51//! ready-to-use data structures without even reading them first.
52//!
53//! Does not require a standard library (`no_std`) or an allocator.
54
55#![expect(incomplete_features, reason = "generic_const_exprs")]
56#![feature(
57    cast_maybe_uninit,
58    const_block_items,
59    const_convert,
60    const_index,
61    const_option_ops,
62    const_result_trait_fn,
63    const_split_off_first_last,
64    const_trait_impl,
65    const_try,
66    generic_const_exprs,
67    ptr_as_uninit
68)]
69#![no_std]
70
71pub mod bool;
72pub mod fixed_capacity_bytes;
73pub mod fixed_capacity_string;
74pub mod maybe_data;
75pub mod metadata;
76pub mod trivial_type;
77pub mod unaligned;
78pub mod variable_bytes;
79pub mod variable_elements;
80
81use crate::trivial_type::TrivialType;
82use core::ops::{Deref, DerefMut};
83use core::ptr::NonNull;
84
85/// The maximum alignment supported by [`IoType`] types (16 bytes, corresponds to alignment of
86/// `u128`)
87pub const MAX_ALIGNMENT: u8 = 16;
88
89const {
90    assert!(
91        size_of::<usize>() >= size_of::<u32>(),
92        "At least 32-bit platform required"
93    );
94
95    // Only support little-endian environments, in big-endian byte order will be different, and
96    // it'll not be possible to simply send bytes of data structures that implement `TrivialType`
97    // from host to guest environment
98    assert!(
99        u16::from_ne_bytes(1u16.to_le_bytes()) == 1u16,
100        "Only little-endian platform is supported"
101    );
102
103    // Max alignment is expected to match that of `u128`
104    assert!(
105        align_of::<u128>() == MAX_ALIGNMENT as usize,
106        "Max alignment mismatch"
107    );
108
109    // Only support targets with expected alignment and refuse to compile on other targets
110    assert!(align_of::<()>() == 1, "Unsupported alignment of `()`");
111    assert!(align_of::<u8>() == 1, "Unsupported alignment of `u8`");
112    assert!(align_of::<u16>() == 2, "Unsupported alignment of `u16`");
113    assert!(align_of::<u32>() == 4, "Unsupported alignment of `u32`");
114    assert!(align_of::<u64>() == 8, "Unsupported alignment of `u64`");
115    assert!(align_of::<u128>() == 16, "Unsupported alignment of `u128`");
116    assert!(align_of::<i8>() == 1, "Unsupported alignment of `i8`");
117    assert!(align_of::<i16>() == 2, "Unsupported alignment of `i16`");
118    assert!(align_of::<i32>() == 4, "Unsupported alignment of `i32`");
119    assert!(align_of::<i64>() == 8, "Unsupported alignment of `i64`");
120    assert!(align_of::<i128>() == 16, "Unsupported alignment of `i128`");
121}
122
123struct DerefWrapper<T>(T);
124
125impl<T> Deref for DerefWrapper<T> {
126    type Target = T;
127
128    #[inline(always)]
129    fn deref(&self) -> &Self::Target {
130        &self.0
131    }
132}
133
134impl<T> DerefMut for DerefWrapper<T> {
135    #[inline(always)]
136    fn deref_mut(&mut self) -> &mut Self::Target {
137        &mut self.0
138    }
139}
140
141// TODO: A way to point output types to input types in order to avoid unnecessary memory copy
142//  (setting a pointer)
143/// Trait that is used for types that are crossing the host/guest boundary in contracts.
144///
145/// Crucially, it is implemented for any type that implements [`TrivialType`] and for
146/// [`VariableBytes`](variable_bytes::VariableBytes).
147///
148/// # Safety
149/// This trait is used for types with memory transmutation capabilities, it must not be relied on
150/// with untrusted data. Serializing and deserializing of types that implement this trait is simply
151/// casting of underlying memory. As a result, all the types implementing this trait must not use
152/// implicit padding, unions, or anything similar that might make it unsound to access any bits of
153/// the type.
154///
155/// Helper functions are provided to make casting to/from bytes a bit safer than it would otherwise,
156/// but extra care is still needed.
157///
158/// **Do not implement this trait explicitly!** Use `#[derive(TrivialType)]` instead, which will
159/// ensure safety requirements are upheld, or use `VariableBytes` or other provided wrapper types if
160/// more flexibility is needed.
161///
162/// In case of variable state size is needed, create a wrapper struct around `VariableBytes` and
163/// implement traits on it by forwarding everything to the inner implementation.
164pub unsafe trait IoType {
165    /// Data structure metadata in binary form, describing shape and types of the contents, see
166    /// [`IoTypeMetadataKind`] for encoding details
167    ///
168    /// [`IoTypeMetadataKind`]: metadata::IoTypeMetadataKind
169    const METADATA: &[u8];
170
171    /// Pointer with a trivial type that this `IoType` represents
172    type PointerType: TrivialType;
173
174    /// Number of bytes that are currently used to store data
175    fn size(&self) -> u32;
176
177    /// Number of bytes are allocated right now
178    fn capacity(&self) -> u32;
179
180    /// Set the number of used bytes
181    ///
182    /// # Safety
183    /// `size` must be set to number of properly initialized bytes
184    unsafe fn set_size(&mut self, size: u32);
185
186    /// Create a reference to a type, which is represented by provided memory.
187    ///
188    /// Memory must be correctly aligned and sufficient in size, but padding beyond the size of the
189    /// type is allowed. Memory behind a pointer must not be written to in the meantime either.
190    ///
191    /// Only `size` bytes are guaranteed to be allocated for types that can store a variable amount
192    /// of data due to the read-only nature of read-only access here.
193    ///
194    /// # Safety
195    /// Input bytes must be previously produced by taking underlying bytes of the same type.
196    // `impl Deref` is used to tie lifetime of returned value to inputs but still treat it as a
197    // shared reference for most practical purposes. While lifetime here is somewhat superficial due
198    // to the `Copy` nature of the value, it must be respected. Size must point to properly
199    // initialized memory.
200    #[track_caller]
201    unsafe fn from_ptr<'a>(
202        ptr: &'a NonNull<Self::PointerType>,
203        size: &'a u32,
204        capacity: u32,
205    ) -> impl Deref<Target = Self> + 'a;
206
207    /// Create a mutable reference to a type, which is represented by provided memory.
208    ///
209    /// Memory must be correctly aligned and sufficient in size, or else `None` will be returned,
210    /// but padding beyond the size of the type is allowed. Memory behind a pointer must not be
211    /// read or written to in the meantime either.
212    ///
213    /// `size` indicates how many bytes are used within a larger allocation for types that can
214    /// store a variable amount of data.
215    ///
216    /// # Safety
217    /// Input bytes must be previously produced by taking underlying bytes of the same type.
218    // `impl DerefMut` is used to tie lifetime of returned value to inputs, but still treat it as an
219    // exclusive reference for most practical purposes. While lifetime here is somewhat superficial
220    // due to the `Copy` nature of the value, it must be respected. Size must point to properly
221    // initialized and aligned memory for non-[`TrivialType`].
222    #[track_caller]
223    unsafe fn from_mut_ptr<'a>(
224        ptr: &'a mut NonNull<Self::PointerType>,
225        size: &'a mut u32,
226        capacity: u32,
227    ) -> impl DerefMut<Target = Self> + 'a;
228
229    /// Get a raw pointer to the underlying data with no checks.
230    ///
231    /// # Safety
232    /// While calling this function is technically safe, it and allows to ignore many of its
233    /// invariants, so requires extra care. In particular, no modifications must be done to the
234    /// value while this returned pointer might be used and no changes must be done through the
235    /// returned pointer. Also, lifetimes are only superficial here and can be easily (and
236    /// incorrectly) ignored by using `Copy`.
237    unsafe fn as_ptr(&self) -> impl Deref<Target = NonNull<Self::PointerType>>;
238
239    /// Get an exclusive raw pointer to the underlying data with no checks.
240    ///
241    /// # Safety
242    /// While calling this function is technically safe, it and allows to ignore many of its
243    /// invariants, so requires extra care. In particular, the value's contents must not be read or
244    /// written to while returned point might be used. Also, lifetimes are only superficial here and
245    /// can be easily (and incorrectly) ignored by using `Copy`.
246    unsafe fn as_mut_ptr(&mut self) -> impl DerefMut<Target = NonNull<Self::PointerType>>;
247}
248
249/// Marker trait, companion to [`IoType`] that indicates the ability to store optional contents.
250///
251/// This means that zero bytes size is a valid invariant. This type is never implemented for types
252/// implementing [`TrivialType`] because they always have fixed size, and it is not zero.
253pub trait IoTypeOptional: IoType {}