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
89// Only little-endian platforms are supported. On big-endian platforms the byte order differs,
90// so `TrivialType` values cannot be transferred simply by sending their raw struct bytes
91// between host and guest environments
92#[cfg(not(target_endian = "little"))]
93compile_error!("Only little-endian platforms are supported");
94
95const {
96    assert!(
97        size_of::<usize>() >= size_of::<u32>(),
98        "At least 32-bit platform required"
99    );
100
101    // Max alignment is expected to match that of `u128`
102    assert!(
103        align_of::<u128>() == MAX_ALIGNMENT as usize,
104        "Max alignment mismatch"
105    );
106
107    // Only support targets with expected alignment and refuse to compile on other targets
108    assert!(align_of::<()>() == 1, "Unsupported alignment of `()`");
109    assert!(align_of::<u8>() == 1, "Unsupported alignment of `u8`");
110    assert!(align_of::<u16>() == 2, "Unsupported alignment of `u16`");
111    assert!(align_of::<u32>() == 4, "Unsupported alignment of `u32`");
112    assert!(align_of::<u64>() == 8, "Unsupported alignment of `u64`");
113    assert!(align_of::<u128>() == 16, "Unsupported alignment of `u128`");
114    assert!(align_of::<i8>() == 1, "Unsupported alignment of `i8`");
115    assert!(align_of::<i16>() == 2, "Unsupported alignment of `i16`");
116    assert!(align_of::<i32>() == 4, "Unsupported alignment of `i32`");
117    assert!(align_of::<i64>() == 8, "Unsupported alignment of `i64`");
118    assert!(align_of::<i128>() == 16, "Unsupported alignment of `i128`");
119}
120
121struct DerefWrapper<T>(T);
122
123impl<T> Deref for DerefWrapper<T> {
124    type Target = T;
125
126    #[inline(always)]
127    fn deref(&self) -> &Self::Target {
128        &self.0
129    }
130}
131
132impl<T> DerefMut for DerefWrapper<T> {
133    #[inline(always)]
134    fn deref_mut(&mut self) -> &mut Self::Target {
135        &mut self.0
136    }
137}
138
139// TODO: A way to point output types to input types in order to avoid unnecessary memory copy
140//  (setting a pointer)
141/// Trait that is used for types that are crossing the host/guest boundary in contracts.
142///
143/// Crucially, it is implemented for any type that implements [`TrivialType`] and for
144/// [`VariableBytes`](variable_bytes::VariableBytes).
145///
146/// # Safety
147/// This trait is used for types with memory transmutation capabilities, it must not be relied on
148/// with untrusted data. Serializing and deserializing of types that implement this trait is simply
149/// casting of underlying memory. As a result, all the types implementing this trait must not use
150/// implicit padding, unions, or anything similar that might make it unsound to access any bits of
151/// the type.
152///
153/// Helper functions are provided to make casting to/from bytes a bit safer than it would otherwise,
154/// but extra care is still needed.
155///
156/// **Do not implement this trait explicitly!** Use `#[derive(TrivialType)]` instead, which will
157/// ensure safety requirements are upheld, or use `VariableBytes` or other provided wrapper types if
158/// more flexibility is needed.
159///
160/// In case of variable state size is needed, create a wrapper struct around `VariableBytes` and
161/// implement traits on it by forwarding everything to the inner implementation.
162pub unsafe trait IoType {
163    /// Data structure metadata in binary form, describing shape and types of the contents, see
164    /// [`IoTypeMetadataKind`] for encoding details
165    ///
166    /// [`IoTypeMetadataKind`]: metadata::IoTypeMetadataKind
167    const METADATA: &[u8];
168
169    /// Pointer with a trivial type that this `IoType` represents
170    type PointerType: TrivialType;
171
172    /// Number of bytes that are currently used to store data
173    fn size(&self) -> u32;
174
175    /// Number of bytes are allocated right now
176    fn capacity(&self) -> u32;
177
178    /// Set the number of used bytes
179    ///
180    /// # Safety
181    /// `size` must be set to number of properly initialized bytes
182    unsafe fn set_size(&mut self, size: u32);
183
184    /// Create a reference to a type, which is represented by provided memory.
185    ///
186    /// Memory must be correctly aligned and sufficient in size, but padding beyond the size of the
187    /// type is allowed. Memory behind a pointer must not be written to in the meantime either.
188    ///
189    /// Only `size` bytes are guaranteed to be allocated for types that can store a variable amount
190    /// of data due to the read-only nature of read-only access here.
191    ///
192    /// # Safety
193    /// Input bytes must be previously produced by taking underlying bytes of the same type.
194    // `impl Deref` is used to tie lifetime of returned value to inputs but still treat it as a
195    // shared reference for most practical purposes. While lifetime here is somewhat superficial due
196    // to the `Copy` nature of the value, it must be respected. Size must point to properly
197    // initialized memory.
198    #[track_caller]
199    unsafe fn from_ptr<'a>(
200        ptr: &'a NonNull<Self::PointerType>,
201        size: &'a u32,
202        capacity: u32,
203    ) -> impl Deref<Target = Self> + 'a;
204
205    /// Create a mutable reference to a type, which is represented by provided memory.
206    ///
207    /// Memory must be correctly aligned and sufficient in size, or else `None` will be returned,
208    /// but padding beyond the size of the type is allowed. Memory behind a pointer must not be
209    /// read or written to in the meantime either.
210    ///
211    /// `size` indicates how many bytes are used within a larger allocation for types that can
212    /// store a variable amount of data.
213    ///
214    /// # Safety
215    /// Input bytes must be previously produced by taking underlying bytes of the same type.
216    // `impl DerefMut` is used to tie lifetime of returned value to inputs, but still treat it as an
217    // exclusive reference for most practical purposes. While lifetime here is somewhat superficial
218    // due to the `Copy` nature of the value, it must be respected. Size must point to properly
219    // initialized and aligned memory for non-[`TrivialType`].
220    #[track_caller]
221    unsafe fn from_mut_ptr<'a>(
222        ptr: &'a mut NonNull<Self::PointerType>,
223        size: &'a mut u32,
224        capacity: u32,
225    ) -> impl DerefMut<Target = Self> + 'a;
226
227    /// Get a raw pointer to the underlying data with no checks.
228    ///
229    /// # Safety
230    /// While calling this function is technically safe, it and allows to ignore many of its
231    /// invariants, so requires extra care. In particular, no modifications must be done to the
232    /// value while this returned pointer might be used and no changes must be done through the
233    /// returned pointer. Also, lifetimes are only superficial here and can be easily (and
234    /// incorrectly) ignored by using `Copy`.
235    unsafe fn as_ptr(&self) -> impl Deref<Target = NonNull<Self::PointerType>>;
236
237    /// Get an exclusive raw pointer to the underlying data with no checks.
238    ///
239    /// # Safety
240    /// While calling this function is technically safe, it and allows to ignore many of its
241    /// invariants, so requires extra care. In particular, the value's contents must not be read or
242    /// written to while returned point might be used. Also, lifetimes are only superficial here and
243    /// can be easily (and incorrectly) ignored by using `Copy`.
244    unsafe fn as_mut_ptr(&mut self) -> impl DerefMut<Target = NonNull<Self::PointerType>>;
245}
246
247/// Marker trait, companion to [`IoType`] that indicates the ability to store optional contents.
248///
249/// This means that zero bytes size is a valid invariant. This type is never implemented for types
250/// implementing [`TrivialType`] because they always have fixed size, and it is not zero.
251pub trait IoTypeOptional: IoType {}