fixed_map/raw.rs
1//! Raw access to underlying storage.
2//!
3//! This can be useful to implement more efficient serialization, since it might
4//! provide access to smaller primitive values.
5
6/// Trait implemented for storage which can be easily converted to and from a
7/// raw value.
8///
9/// This is implemented for [`SetStorage`] when the `#[key(bitset)]` attribute
10/// is present.
11///
12/// [`SetStorage`]: crate::set::SetStorage
13pub trait RawStorage: Sized {
14 /// The backing raw value.
15 type Value;
16
17 /// Get the raw value of the storage.
18 fn as_raw(&self) -> Self::Value;
19
20 /// Build storage from raw storage.
21 fn from_raw(raw: Self::Value) -> Self;
22}
23
24/// Raw storage which is laid out exactly like its [`RawStorage::Value`].
25///
26/// This is implemented for [`SetStorage`] when the `#[key(bitset)]` attribute
27/// is present, and lets [`Set`] implement layout-dependent traits like
28/// `ZeroCopy` from `musli-zerocopy`.
29///
30/// # Safety
31///
32/// The implementor must be `#[repr(transparent)]` over a single field of type
33/// [`RawStorage::Value`], every bit pattern of that value must be a valid
34/// instance of the storage, and [`RawStorage::as_raw`] and
35/// [`RawStorage::from_raw`] must copy that field unchanged.
36///
37/// [`SetStorage`]: crate::set::SetStorage
38/// [`Set`]: crate::Set
39pub unsafe trait TransparentRawStorage: RawStorage {}