Trait zerovec::ule::EncodeAsVarULE

source ·
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::as_byte_slice() 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.

§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 the T VarULE type (i.e. if fed to VarULE::validate_byte_slice() they must produce a successful result)
  • It must return the return value of cb to 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 dst must be a valid instance of the T VarULE type

Required Methods§

source

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§

source

fn encode_var_ule_len(&self) -> usize

Return the length, in bytes, of the corresponding VarULE type

source

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

Object Safety§

This trait is not object safe.

Implementations on Foreign Types§

source§

impl EncodeAsVarULE<str> for &String

source§

fn encode_var_ule_as_slices<R>(&self, cb: impl FnOnce(&[&[u8]]) -> R) -> R

source§

impl EncodeAsVarULE<str> for String

source§

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,

source§

fn encode_var_ule_as_slices<R>(&self, _: impl FnOnce(&[&[u8]]) -> R) -> R

source§

fn encode_var_ule_len(&self) -> usize

source§

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,

source§

fn encode_var_ule_as_slices<R>(&self, _: impl FnOnce(&[&[u8]]) -> R) -> R

source§

fn encode_var_ule_len(&self) -> usize

source§

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,

source§

fn encode_var_ule_as_slices<R>(&self, _: impl FnOnce(&[&[u8]]) -> R) -> R

source§

fn encode_var_ule_len(&self) -> usize

source§

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,

source§

fn encode_var_ule_as_slices<R>(&self, _: impl FnOnce(&[&[u8]]) -> R) -> R

source§

fn encode_var_ule_len(&self) -> usize

source§

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,

source§

fn encode_var_ule_as_slices<R>(&self, _: impl FnOnce(&[&[u8]]) -> R) -> R

source§

fn encode_var_ule_len(&self) -> usize

source§

fn encode_var_ule_write(&self, dst: &mut [u8])

source§

impl<T> EncodeAsVarULE<[T]> for Vec<T>
where T: ULE,

source§

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,

source§

fn encode_var_ule_as_slices<R>(&self, _: impl FnOnce(&[&[u8]]) -> R) -> R

source§

fn encode_var_ule_len(&self) -> usize

source§

fn encode_var_ule_write(&self, dst: &mut [u8])

source§

impl<T> EncodeAsVarULE<ZeroSlice<T>> for Vec<T>
where T: AsULE + 'static,

source§

fn encode_var_ule_as_slices<R>(&self, _: impl FnOnce(&[&[u8]]) -> R) -> R

source§

fn encode_var_ule_len(&self) -> usize

source§

fn encode_var_ule_write(&self, dst: &mut [u8])

source§

impl<T> EncodeAsVarULE<T> for Cow<'_, T>
where T: ToOwned + VarULE + ?Sized,

source§

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]

source§

fn encode_var_ule_as_slices<R>(&self, _: impl FnOnce(&[&[u8]]) -> R) -> R

source§

fn encode_var_ule_len(&self) -> usize

source§

fn encode_var_ule_write(&self, dst: &mut [u8])

source§

impl<T, E, F> EncodeAsVarULE<VarZeroSlice<T, F>> for Vec<E>

source§

fn encode_var_ule_as_slices<R>(&self, _: impl FnOnce(&[&[u8]]) -> R) -> R

source§

fn encode_var_ule_len(&self) -> usize

source§

fn encode_var_ule_write(&self, dst: &mut [u8])

source§

impl<T, U> EncodeAsVarULE<OptionVarULE<U>> for Option<T>
where T: EncodeAsVarULE<U>, U: VarULE + ?Sized,

source§

fn encode_var_ule_as_slices<R>(&self, _: impl FnOnce(&[&[u8]]) -> R) -> R

source§

fn encode_var_ule_len(&self) -> usize

source§

fn encode_var_ule_write(&self, dst: &mut [u8])

source§

impl<T: VarULE + ?Sized> EncodeAsVarULE<T> for &&T

source§

fn encode_var_ule_as_slices<R>(&self, cb: impl FnOnce(&[&[u8]]) -> R) -> R

source§

impl<T: VarULE + ?Sized> EncodeAsVarULE<T> for &Box<T>

source§

fn encode_var_ule_as_slices<R>(&self, cb: impl FnOnce(&[&[u8]]) -> R) -> R

source§

impl<T: VarULE + ?Sized> EncodeAsVarULE<T> for &T

source§

fn encode_var_ule_as_slices<R>(&self, cb: impl FnOnce(&[&[u8]]) -> R) -> R

source§

impl<T: VarULE + ?Sized> EncodeAsVarULE<T> for Box<T>

source§

fn encode_var_ule_as_slices<R>(&self, cb: impl FnOnce(&[&[u8]]) -> R) -> R

Implementors§

source§

impl<'a, V: VarULE + ?Sized> EncodeAsVarULE<V> for VarZeroCow<'a, V>

source§

impl<A, B, V> EncodeAsVarULE<VarTupleULE<A, V>> for VarTuple<A, B>
where A: AsULE + 'static, B: EncodeAsVarULE<V>, V: VarULE + ?Sized,

source§

impl<T> EncodeAsVarULE<ZeroSlice<T>> for ZeroVec<'_, T>
where T: AsULE + 'static,

source§

impl<T, F> EncodeAsVarULE<VarZeroSlice<T, F>> for VarZeroVec<'_, T, F>
where T: VarULE + ?Sized, F: VarZeroVecFormat,

source§

impl<T: VarULE + ?Sized> EncodeAsVarULE<T> for T