Trait EncodeAsVarULE
unsafe trait EncodeAsVarULE<T: VarULE + ?Sized>
Allows types to be encoded as VarULEs. This is highly useful for implementing VarULE on custom DSTs where the type cannot be obtained as a reference to some other type.
[Self::encode_var_ule_as_slices()] should be implemented by providing an encoded slice for each field
of the VarULE type to the callback, in order. For an implementation to be safe, the slices
to the callback must, when concatenated, be a valid instance of the VarULE type.
See the custom VarULEdocumentation for examples.
[Self::encode_var_ule_as_slices()] is only used to provide default implementations for [Self::encode_var_ule_write()]
and [Self::encode_var_ule_len()]. If you override the default implementations it is totally valid to
replace [Self::encode_var_ule_as_slices()]'s body with unreachable!(). This can be done for cases where
it is not possible to implement [Self::encode_var_ule_as_slices()] but the other methods still work.
A typical implementation will take each field in the order found in the VarULE type,
convert it to ULE, call [ULE::slice_as_bytes()] on them, and pass the slices to cb in order.
A trailing ZeroVec or VarZeroVec can have their underlying
byte representation passed through.
In case the compiler is not optimizing [Self::encode_var_ule_len()], it can be overridden. A typical
implementation will add up the sizes of each field on the VarULE type and then add in the byte length of the
dynamically-sized part.
Reverse-encoding VarULE
This trait maps a struct to its bytes representation ("serialization"), and
ZeroFrom performs the opposite operation, taking those bytes and
creating a struct from them ("deserialization").
Safety
The safety invariants of [Self::encode_var_ule_as_slices()] are:
- It must call
cb(only once) - The slices passed to
cb, if concatenated, should be a valid instance of theTVarULEtype (i.e. if fed to [VarULE::validate_bytes()] they must produce a successful result) - It must return the return value of
cbto the caller
One or more of [Self::encode_var_ule_len()] and [Self::encode_var_ule_write()] may be provided.
If both are, then zerovec code is guaranteed to not call [Self::encode_var_ule_as_slices()], and it may be replaced
with unreachable!().
The safety invariants of [Self::encode_var_ule_len()] are:
- It must return the length of the corresponding VarULE type
The safety invariants of [Self::encode_var_ule_write()] are:
- The slice written to
dstmust be a valid instance of theTVarULEtype
Required Methods
fn encode_var_ule_as_slices<R, impl FnOnce(&[&[u8]]) -> R: FnOnce(&[&[u8]]) -> R>(self: &Self, cb: impl FnOnce(&[&[u8]]) -> R) -> RCalls
cbwith a piecewise list of byte slices that when concatenated produce the memory pattern of the corresponding instance ofT.Do not call this function directly; instead use the other two. Some implementors may define this function to panic.
Provided Methods
Implementors
impl<T> EncodeAsVarULE for Vec<T>impl<T> EncodeAsVarULE for Tuple6VarULE<A, B, C, D, E, F, Format>impl<A, B, C, D, AE, BE, CE, DE, Format> EncodeAsVarULE for (AE, BE, CE, DE)impl<T: VarULE + ?Sized> EncodeAsVarULE for Box<T>impl<T: VarULE + ?Sized> EncodeAsVarULE for Timpl<T> EncodeAsVarULE for Tuple5VarULE<A, B, C, D, E, Format>impl<A, B, C, AE, BE, CE, Format> EncodeAsVarULE for (AE, BE, CE)impl<'a, V: VarULE + ?Sized> EncodeAsVarULE for VarZeroCow<'a, V>impl<T> EncodeAsVarULE for Cow<'_, T>impl<T> EncodeAsVarULE for ZeroSlice<T>impl<A, B, AE, BE, Format> EncodeAsVarULE for (AE, BE)impl<T, E, F> EncodeAsVarULE for Vec<E>impl<T> EncodeAsVarULE for Vec<T>impl<T> EncodeAsVarULE for ZeroVec<'_, T>impl<T> EncodeAsVarULE for Tuple3VarULE<A, B, C, Format>impl<T> EncodeAsVarULE for VarZeroSlice<T, F>impl<T> EncodeAsVarULE for &[T]impl EncodeAsVarULE for Stringimpl<T> EncodeAsVarULE for OptionVarULE<U>impl<T: VarULE + ?Sized> EncodeAsVarULE for &&Timpl<A, B, V> EncodeAsVarULE for VarTuple<A, B>impl EncodeAsVarULE for &Stringimpl<T> EncodeAsVarULE for VarTupleULE<A, V>impl<A, B, C, D, E, F, AE, BE, CE, DE, EE, FE, Format> EncodeAsVarULE for (AE, BE, CE, DE, EE, FE)impl<T: VarULE + ?Sized> EncodeAsVarULE for &Box<T>impl<T> EncodeAsVarULE for Tuple2VarULE<A, B, Format>impl<T: VarULE + ?Sized> EncodeAsVarULE for &Timpl<T, F> EncodeAsVarULE for VarZeroVec<'_, T, F>impl<T> EncodeAsVarULE for MultiFieldsULE<LEN, Format>impl<T, U> EncodeAsVarULE for Option<T>impl<T, E, F> EncodeAsVarULE for &[E]impl<A, B, C, D, E, AE, BE, CE, DE, EE, Format> EncodeAsVarULE for (AE, BE, CE, DE, EE)impl<T> EncodeAsVarULE for Tuple4VarULE<A, B, C, D, Format>