Skip to main content

InPlaceCodec

Trait InPlaceCodec 

Source
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_bytes initializes every one of the ENCODED_SIZE bytes of out, without reading out (it may be uninitialized memory).
  • read_le_bytes is unbiased: given bytes.len() == ENCODED_SIZE, if write_le_bytes could have produced bytes, read_le_bytes must return the same value.

Required Associated Constants§

Source

const ENCODED_SIZE: usize

Encoded width in bytes.

Provided Associated Constants§

Source

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).

Source

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§

Source

fn write_le_bytes(&self, out: &mut [MaybeUninit<u8>])

Write self’s canonical encoding into out, initializing every byte. out.len() == Self::ENCODED_SIZE.

Source

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§

Source

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.

Source

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).

Source

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.

Source

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".

Implementations on Foreign Types§

Source§

impl InPlaceCodec for bool

Source§

impl InPlaceCodec for u8

Source§

impl InPlaceCodec for u16

Source§

impl InPlaceCodec for u32

Source§

impl InPlaceCodec for u64

Source§

impl InPlaceCodec for usize

Source§

impl<A: InPlaceCodec, B: InPlaceCodec> InPlaceCodec for (A, B)

Source§

impl<T: InPlaceCodec, N: ArraySize> InPlaceCodec for Array<T, N>

Source§

impl<T: InPlaceCodec, const N: usize> InPlaceCodec for [T; N]

Source§

impl<T: InPlaceCodec> InPlaceCodec for Arc<T>

Source§

const ENCODED_SIZE: usize = T::ENCODED_SIZE

Source§

fn write_le_bytes(&self, out: &mut [MaybeUninit<u8>])

Source§

fn read_le_bytes(bytes: &[u8]) -> Result<Self, PrimitiveError>

Implementors§

Source§

impl InPlaceCodec for BaseField25519

Source§

impl InPlaceCodec for BaseFieldP384

Source§

impl InPlaceCodec for Gf2

Source§

impl InPlaceCodec for Gf2_128Field

Source§

const ENCODED_SIZE: usize = Gf2_128::ENCODED_SIZE

Source§

impl InPlaceCodec for Mersenne107

Source§

impl InPlaceCodec for PointP384

Source§

impl InPlaceCodec for Scalar25519

Source§

impl InPlaceCodec for ScalarP384

Source§

impl<A: InPlaceCodec, B: InPlaceCodec> InPlaceCodec for PairwiseAuthKey<A, B>

Source§

impl<C: Curve> InPlaceCodec for Point<C>

Source§

impl<F: FieldExtension + InPlaceCodec> InPlaceCodec for FieldElement<F>

Source§

const ENCODED_SIZE: usize = F::ENCODED_SIZE

Source§

const PACK: usize = F::PACK

Source§

const PACK_BYTES: usize = F::PACK_BYTES

Source§

impl<F: FieldExtension> InPlaceCodec for SubfieldElement<F>

Source§

const ENCODED_SIZE: usize = <F::Subfield as InPlaceCodec>::ENCODED_SIZE

Source§

const PACK: usize = <F::Subfield as InPlaceCodec>::PACK

Source§

const PACK_BYTES: usize = <F::Subfield as InPlaceCodec>::PACK_BYTES

Source§

impl<P: Gf2ExtParams, const LIMBS: usize> InPlaceCodec for Gf2Ext<P, LIMBS>

Source§

impl<T: InPlaceCodec, M: Positive> InPlaceCodec for HeapArray<T, M>

Source§

impl<V: InPlaceCodec, Mac: InPlaceCodec> InPlaceCodec for PairwiseAuthOpenShare<V, Mac>