pub unsafe trait InPlaceCodec: Sized {
const ENCODED_SIZE: usize;
const PACK: usize = 1;
const PACK_BYTES: usize = _;
// Required methods
fn write_le_bytes(&self, out: &mut [MaybeUninit<u8>]);
fn read_le_bytes(bytes: &[u8]) -> Result<Self, PrimitiveError>;
// Provided methods
fn write_pack(items: &[Self], out: &mut [MaybeUninit<u8>]) { ... }
fn read_pack(
bytes: &[u8],
out: &mut [MaybeUninit<Self>],
) -> Result<(), PrimitiveError> { ... }
fn to_inplace_bytes(&self) -> Vec<u8> ⓘ { ... }
fn from_inplace_bytes(bytes: &[u8]) -> Result<Self, PrimitiveError> { ... }
}Expand description
A type that can be (de)serialized to/from a fixed-size byte window directly.
The encoding must be architecture-independent (same bytes on any target — in practice
fixed-width little-endian) but is otherwise this codec’s own format: it need not match the
type’s serde::Serialize/Deserialize nor its bincode encoding.
§Safety
Implementors must guarantee:
write_le_bytesinitializes every one of theENCODED_SIZEbytes ofout, without readingout(it may be uninitialized memory).read_le_bytesis unbiased: givenbytes.len() == ENCODED_SIZE, ifwrite_le_bytescould have producedbytes,read_le_bytesmust return the same value.
Required Associated Constants§
Sourceconst ENCODED_SIZE: usize
const ENCODED_SIZE: usize
Encoded width in bytes.
Provided Associated Constants§
Sourceconst PACK: usize = 1
const PACK: usize = 1
Number of consecutive elements that encode together as a “pack”. 1 (the default) means no
grouping — each element is encoded independently via Self::write_le_bytes. Must be >= 1.
Container codecs (e.g. HeapArray<_, N>) split a run of elements into N / PACK full packs
plus an N % PACK per-element tail. Types with PACK > 1 must be Copy (see
Self::read_pack).
Sourceconst PACK_BYTES: usize = _
const PACK_BYTES: usize = _
Bytes a full PACK-element pack occupies. Defaults to PACK * ENCODED_SIZE (the packed
bytes are exactly the per-element bytes laid out back-to-back — the case for a pure
vectorization like Mersenne107). Override with a smaller value for a sub-byte packing
(e.g. Gf2: PACK = 8, PACK_BYTES = 1).
Required Methods§
Sourcefn write_le_bytes(&self, out: &mut [MaybeUninit<u8>])
fn write_le_bytes(&self, out: &mut [MaybeUninit<u8>])
Write self’s canonical encoding into out, initializing every byte.
out.len() == Self::ENCODED_SIZE.
Sourcefn read_le_bytes(bytes: &[u8]) -> Result<Self, PrimitiveError>
fn read_le_bytes(bytes: &[u8]) -> Result<Self, PrimitiveError>
Parse self from bytes, validating it. bytes.len() == Self::ENCODED_SIZE.
Returns an error on an invalid (e.g. non-canonical) encoding.
Provided Methods§
Sourcefn write_pack(items: &[Self], out: &mut [MaybeUninit<u8>])
fn write_pack(items: &[Self], out: &mut [MaybeUninit<u8>])
Encode exactly Self::PACK elements (items.len() == PACK) into out
(out.len() == PACK_BYTES), initializing every byte.
The default encodes the group element-by-element (valid whenever
PACK_BYTES == PACK * ENCODED_SIZE); override it for a vectorized or sub-byte-packed
encoding.
Sourcefn read_pack(
bytes: &[u8],
out: &mut [MaybeUninit<Self>],
) -> Result<(), PrimitiveError>
fn read_pack( bytes: &[u8], out: &mut [MaybeUninit<Self>], ) -> Result<(), PrimitiveError>
Decode a full pack: read Self::PACK elements from bytes (bytes.len() == PACK_BYTES)
into out (out.len() == PACK), returning an error on an invalid encoding.
The default decodes element-by-element (valid whenever PACK_BYTES == PACK * ENCODED_SIZE).
On an error it may leave some of out written; the container caller drops only completed
packs, so any type with PACK > 1 must be Copy (partially-written packs are not dropped).
Sourcefn to_inplace_bytes(&self) -> Vec<u8> ⓘ
fn to_inplace_bytes(&self) -> Vec<u8> ⓘ
Serialize self into a freshly-allocated buffer of exactly Self::ENCODED_SIZE bytes,
bypassing per-element serde dispatch.
Sourcefn from_inplace_bytes(bytes: &[u8]) -> Result<Self, PrimitiveError>
fn from_inplace_bytes(bytes: &[u8]) -> Result<Self, PrimitiveError>
Deserialize Self from exactly Self::ENCODED_SIZE bytes.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".