pub unsafe trait EncodeAsVarULE<T: VarULE + ?Sized> {
// Required method
fn encode_var_ule_as_slices<R>(&self, cb: impl FnOnce(&[&[u8]]) -> R) -> R;
// Provided methods
fn encode_var_ule_len(&self) -> usize { ... }
fn encode_var_ule_write(&self, dst: &mut [u8]) { ... }
}Expand description
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 toVarULE::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§
Sourcefn encode_var_ule_as_slices<R>(&self, cb: impl FnOnce(&[&[u8]]) -> R) -> R
fn encode_var_ule_as_slices<R>(&self, cb: impl FnOnce(&[&[u8]]) -> R) -> R
Calls cb with a piecewise list of byte slices that when concatenated
produce the memory pattern of the corresponding instance of T.
Do not call this function directly; instead use the other two. Some implementors may define this function to panic.
Provided Methods§
Sourcefn encode_var_ule_len(&self) -> usize
fn encode_var_ule_len(&self) -> usize
Return the length, in bytes, of the corresponding VarULE type
Sourcefn encode_var_ule_write(&self, dst: &mut [u8])
fn encode_var_ule_write(&self, dst: &mut [u8])
Write the corresponding VarULE type to the dst buffer. dst should
be the size of Self::encode_var_ule_len()
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.
Implementations on Foreign Types§
Source§impl EncodeAsVarULE<str> for &String
Available on crate feature alloc only.
impl EncodeAsVarULE<str> for &String
alloc only.fn encode_var_ule_as_slices<R>(&self, cb: impl FnOnce(&[&[u8]]) -> R) -> R
Source§impl EncodeAsVarULE<str> for String
Available on crate feature alloc only.
impl EncodeAsVarULE<str> for String
alloc only.fn encode_var_ule_as_slices<R>(&self, cb: impl FnOnce(&[&[u8]]) -> R) -> R
Source§impl<A, B, AE, BE, Format> EncodeAsVarULE<Tuple2VarULE<A, B, Format>> for (AE, BE)where
A: VarULE + ?Sized,
B: VarULE + ?Sized,
AE: EncodeAsVarULE<A>,
BE: EncodeAsVarULE<B>,
Format: VarZeroVecFormat,
impl<A, B, AE, BE, Format> EncodeAsVarULE<Tuple2VarULE<A, B, Format>> for (AE, BE)where
A: VarULE + ?Sized,
B: VarULE + ?Sized,
AE: EncodeAsVarULE<A>,
BE: EncodeAsVarULE<B>,
Format: VarZeroVecFormat,
fn encode_var_ule_as_slices<R>(&self, _: impl FnOnce(&[&[u8]]) -> R) -> R
fn encode_var_ule_len(&self) -> usize
fn encode_var_ule_write(&self, dst: &mut [u8])
Source§impl<A, B, C, AE, BE, CE, Format> EncodeAsVarULE<Tuple3VarULE<A, B, C, Format>> for (AE, BE, CE)where
A: VarULE + ?Sized,
B: VarULE + ?Sized,
C: VarULE + ?Sized,
AE: EncodeAsVarULE<A>,
BE: EncodeAsVarULE<B>,
CE: EncodeAsVarULE<C>,
Format: VarZeroVecFormat,
impl<A, B, C, AE, BE, CE, Format> EncodeAsVarULE<Tuple3VarULE<A, B, C, Format>> for (AE, BE, CE)where
A: VarULE + ?Sized,
B: VarULE + ?Sized,
C: VarULE + ?Sized,
AE: EncodeAsVarULE<A>,
BE: EncodeAsVarULE<B>,
CE: EncodeAsVarULE<C>,
Format: VarZeroVecFormat,
fn encode_var_ule_as_slices<R>(&self, _: impl FnOnce(&[&[u8]]) -> R) -> R
fn encode_var_ule_len(&self) -> usize
fn encode_var_ule_write(&self, dst: &mut [u8])
Source§impl<A, B, C, D, AE, BE, CE, DE, Format> EncodeAsVarULE<Tuple4VarULE<A, B, C, D, Format>> for (AE, BE, CE, DE)where
A: VarULE + ?Sized,
B: VarULE + ?Sized,
C: VarULE + ?Sized,
D: VarULE + ?Sized,
AE: EncodeAsVarULE<A>,
BE: EncodeAsVarULE<B>,
CE: EncodeAsVarULE<C>,
DE: EncodeAsVarULE<D>,
Format: VarZeroVecFormat,
impl<A, B, C, D, AE, BE, CE, DE, Format> EncodeAsVarULE<Tuple4VarULE<A, B, C, D, Format>> for (AE, BE, CE, DE)where
A: VarULE + ?Sized,
B: VarULE + ?Sized,
C: VarULE + ?Sized,
D: VarULE + ?Sized,
AE: EncodeAsVarULE<A>,
BE: EncodeAsVarULE<B>,
CE: EncodeAsVarULE<C>,
DE: EncodeAsVarULE<D>,
Format: VarZeroVecFormat,
fn encode_var_ule_as_slices<R>(&self, _: impl FnOnce(&[&[u8]]) -> R) -> R
fn encode_var_ule_len(&self) -> usize
fn encode_var_ule_write(&self, dst: &mut [u8])
Source§impl<A, B, C, D, E, AE, BE, CE, DE, EE, Format> EncodeAsVarULE<Tuple5VarULE<A, B, C, D, E, Format>> for (AE, BE, CE, DE, EE)where
A: VarULE + ?Sized,
B: VarULE + ?Sized,
C: VarULE + ?Sized,
D: VarULE + ?Sized,
E: VarULE + ?Sized,
AE: EncodeAsVarULE<A>,
BE: EncodeAsVarULE<B>,
CE: EncodeAsVarULE<C>,
DE: EncodeAsVarULE<D>,
EE: EncodeAsVarULE<E>,
Format: VarZeroVecFormat,
impl<A, B, C, D, E, AE, BE, CE, DE, EE, Format> EncodeAsVarULE<Tuple5VarULE<A, B, C, D, E, Format>> for (AE, BE, CE, DE, EE)where
A: VarULE + ?Sized,
B: VarULE + ?Sized,
C: VarULE + ?Sized,
D: VarULE + ?Sized,
E: VarULE + ?Sized,
AE: EncodeAsVarULE<A>,
BE: EncodeAsVarULE<B>,
CE: EncodeAsVarULE<C>,
DE: EncodeAsVarULE<D>,
EE: EncodeAsVarULE<E>,
Format: VarZeroVecFormat,
fn encode_var_ule_as_slices<R>(&self, _: impl FnOnce(&[&[u8]]) -> R) -> R
fn encode_var_ule_len(&self) -> usize
fn encode_var_ule_write(&self, dst: &mut [u8])
Source§impl<A, B, C, D, E, F, AE, BE, CE, DE, EE, FE, Format> EncodeAsVarULE<Tuple6VarULE<A, B, C, D, E, F, Format>> for (AE, BE, CE, DE, EE, FE)where
A: VarULE + ?Sized,
B: VarULE + ?Sized,
C: VarULE + ?Sized,
D: VarULE + ?Sized,
E: VarULE + ?Sized,
F: VarULE + ?Sized,
AE: EncodeAsVarULE<A>,
BE: EncodeAsVarULE<B>,
CE: EncodeAsVarULE<C>,
DE: EncodeAsVarULE<D>,
EE: EncodeAsVarULE<E>,
FE: EncodeAsVarULE<F>,
Format: VarZeroVecFormat,
impl<A, B, C, D, E, F, AE, BE, CE, DE, EE, FE, Format> EncodeAsVarULE<Tuple6VarULE<A, B, C, D, E, F, Format>> for (AE, BE, CE, DE, EE, FE)where
A: VarULE + ?Sized,
B: VarULE + ?Sized,
C: VarULE + ?Sized,
D: VarULE + ?Sized,
E: VarULE + ?Sized,
F: VarULE + ?Sized,
AE: EncodeAsVarULE<A>,
BE: EncodeAsVarULE<B>,
CE: EncodeAsVarULE<C>,
DE: EncodeAsVarULE<D>,
EE: EncodeAsVarULE<E>,
FE: EncodeAsVarULE<F>,
Format: VarZeroVecFormat,
fn encode_var_ule_as_slices<R>(&self, _: impl FnOnce(&[&[u8]]) -> R) -> R
fn encode_var_ule_len(&self) -> usize
fn encode_var_ule_write(&self, dst: &mut [u8])
Source§impl<T> EncodeAsVarULE<[T]> for Vec<T>where
T: ULE,
Available on crate feature alloc only.
impl<T> EncodeAsVarULE<[T]> for Vec<T>where
T: ULE,
alloc only.fn encode_var_ule_as_slices<R>(&self, cb: impl FnOnce(&[&[u8]]) -> R) -> R
Source§impl<T> EncodeAsVarULE<ZeroSlice<T>> for &[T]where
T: AsULE + 'static,
impl<T> EncodeAsVarULE<ZeroSlice<T>> for &[T]where
T: AsULE + 'static,
fn encode_var_ule_as_slices<R>(&self, _: impl FnOnce(&[&[u8]]) -> R) -> R
fn encode_var_ule_len(&self) -> usize
fn encode_var_ule_write(&self, dst: &mut [u8])
Source§impl<T> EncodeAsVarULE<ZeroSlice<T>> for Vec<T>where
T: AsULE + 'static,
Available on crate feature alloc only.
impl<T> EncodeAsVarULE<ZeroSlice<T>> for Vec<T>where
T: AsULE + 'static,
alloc only.fn encode_var_ule_as_slices<R>(&self, _: impl FnOnce(&[&[u8]]) -> R) -> R
fn encode_var_ule_len(&self) -> usize
fn encode_var_ule_write(&self, dst: &mut [u8])
Source§impl<T> EncodeAsVarULE<T> for Cow<'_, T>
Available on crate feature alloc only.
impl<T> EncodeAsVarULE<T> for Cow<'_, T>
alloc only.fn encode_var_ule_as_slices<R>(&self, cb: impl FnOnce(&[&[u8]]) -> R) -> R
Source§impl<T, E, F> EncodeAsVarULE<VarZeroSlice<T, F>> for &[E]
impl<T, E, F> EncodeAsVarULE<VarZeroSlice<T, F>> for &[E]
fn encode_var_ule_as_slices<R>(&self, _: impl FnOnce(&[&[u8]]) -> R) -> R
fn encode_var_ule_len(&self) -> usize
fn encode_var_ule_write(&self, dst: &mut [u8])
Source§impl<T, E, F> EncodeAsVarULE<VarZeroSlice<T, F>> for Vec<E>
Available on crate feature alloc only.
impl<T, E, F> EncodeAsVarULE<VarZeroSlice<T, F>> for Vec<E>
alloc only.fn encode_var_ule_as_slices<R>(&self, _: impl FnOnce(&[&[u8]]) -> R) -> R
fn encode_var_ule_len(&self) -> usize
fn encode_var_ule_write(&self, dst: &mut [u8])
Source§impl<T, U> EncodeAsVarULE<OptionVarULE<U>> for Option<T>
impl<T, U> EncodeAsVarULE<OptionVarULE<U>> for Option<T>
fn encode_var_ule_as_slices<R>(&self, _: impl FnOnce(&[&[u8]]) -> R) -> R
fn encode_var_ule_len(&self) -> usize
fn encode_var_ule_write(&self, dst: &mut [u8])
Source§impl<T: VarULE + ?Sized> EncodeAsVarULE<T> for &&T
impl<T: VarULE + ?Sized> EncodeAsVarULE<T> for &&T
fn encode_var_ule_as_slices<R>(&self, cb: impl FnOnce(&[&[u8]]) -> R) -> R
Source§impl<T: VarULE + ?Sized> EncodeAsVarULE<T> for &Box<T>
Available on crate feature alloc only.
impl<T: VarULE + ?Sized> EncodeAsVarULE<T> for &Box<T>
alloc only.fn encode_var_ule_as_slices<R>(&self, cb: impl FnOnce(&[&[u8]]) -> R) -> R
Source§impl<T: VarULE + ?Sized> EncodeAsVarULE<T> for &T
impl<T: VarULE + ?Sized> EncodeAsVarULE<T> for &T
fn encode_var_ule_as_slices<R>(&self, cb: impl FnOnce(&[&[u8]]) -> R) -> R
Source§impl<T: VarULE + ?Sized> EncodeAsVarULE<T> for Box<T>
Available on crate feature alloc only.
impl<T: VarULE + ?Sized> EncodeAsVarULE<T> for Box<T>
alloc only.