usecrate::ule::*; usecrate::varzerovec::VarZeroVecFormat; usecrate::{VarZeroSlice, VarZeroVec, ZeroSlice, ZeroVec}; use alloc::borrow::{Cow, ToOwned}; use alloc::boxed::Box; use alloc::string::String; use alloc::{vec, vec::Vec}; use core::mem;
/// 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](crate::ule::custom) 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`](crate::ZeroVec) or [`VarZeroVec`](crate::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 pubunsafetrait EncodeAsVarULE<T: VarULE + ?Sized> { /// 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. fn encode_var_ule_as_slices<R>(&self, cb: impl FnOnce(&[&[u8]]) -> R) -> R;
/// Return the length, in bytes, of the corresponding [`VarULE`] type fn encode_var_ule_len(&self) -> usize { self.encode_var_ule_as_slices(|slices| slices.iter().map(|s| s.len()).sum())
}
/// Write the corresponding [`VarULE`] type to the `dst` buffer. `dst` should /// be the size of [`Self::encode_var_ule_len()`] fn encode_var_ule_write(&self, mut dst: &>mut [u8]) {
debug_assert_eq!(self.encode_var_ule_len(), dst.len()); self.encode_var_ule_as_slices(move |slices| { #[allow(clippy::indexing_slicing)] // by debug_assert for slice in slices {
dst[..slice.len()].copy_from_slice(slice);
dst = &mut dst[slice.len()..];
}
});
}
}
/// Given an [`EncodeAsVarULE`] type `S`, encode it into a `Box<T>` /// /// This is primarily useful for generating `Deserialize` impls for VarULE types pubfn encode_varule_to_box<S: EncodeAsVarULE<T>, T: VarULE + ?Sized>(x: &S) -> Box<T> { // zero-fill the vector to avoid uninitialized data UB letmut vec: Vec<u8> = vec![0; x.encode_var_ule_len()];
x.encode_var_ule_write(&mut vec); let boxed = mem::ManuallyDrop::new(vec.into_boxed_slice()); unsafe { // Safety: `ptr` is a box, and `T` is a VarULE which guarantees it has the same memory layout as `[u8]` // and can be recouped via from_byte_slice_unchecked() let ptr: *mut T = T::from_byte_slice_unchecked(&boxed) as *const T as *mut T;
// Safety: we can construct an owned version since we have mem::forgotten the older owner Box::from_raw(ptr)
}
}
unsafeimpl<T: VarULE + ?Sized> EncodeAsVarULE<T> for T { fn encode_var_ule_as_slices<R>(&self, cb: impl FnOnce(&[&[u8]]) -> R) -> R {
cb(&[T::as_byte_slice(self)])
}
}
unsafeimpl<T: VarULE + ?Sized> EncodeAsVarULE<T> for &'_ T { fn encode_var_ule_as_slices<R>(&self, cb: impl FnOnce(&[&[u8]]) -> R) -> R {
cb(&[T::as_byte_slice(self)])
}
}
unsafeimpl<T: VarULE + ?Sized> EncodeAsVarULE<T> for Cow<'_, T> where
T: ToOwned,
{ fn encode_var_ule_as_slices<R>(&self, cb: impl FnOnce(&[&[u8]]) -> R) -> R {
cb(&[T::as_byte_slice(self.as_ref())])
}
}
unsafeimpl EncodeAsVarULE<str> for String { fn encode_var_ule_as_slices<R>(&self, cb: impl FnOnce(&[&[u8]]) -> R) -> R {
cb(&[self.as_bytes()])
}
}
// Note: This impl could technically use `T: AsULE`, but we want users to prefer `ZeroSlice<T>` // for cases where T is not a ULE. Therefore, we can use the more efficient `memcpy` impl here. unsafeimpl<T> EncodeAsVarULE<[T]> for Vec<T> where
T: ULE,
{ fn encode_var_ule_as_slices<R>(&self, cb: impl FnOnce(&[&[u8]]) -> R) -> R {
cb(&[<[T] as VarULE>::as_byte_slice(self)])
}
}
unsafeimpl<T> EncodeAsVarULE<ZeroSlice<T>> for &'_ [T] where
T: AsULE + 'static,
{ fn encode_var_ule_as_slices<R>(&self, _: impl FnOnce(&[&[u8]]) -> R) -> R { // unnecessary if the other two are implemented
unreachable!()
}
fn encode_var_ule_write(&self, dst: &mut [u8]) { #[allow(non_snake_case)] let S = core::mem::size_of::<T::ULE>();
debug_assert_eq!(self.len() * S, dst.len()); for (item, refmut chunk) inself.iter().zip(dst.chunks_mut(S)) { let ule = item.to_unaligned();
chunk.copy_from_slice(ULE::as_byte_slice(core::slice::from_ref(&ule)));
}
}
}
unsafeimpl<T> EncodeAsVarULE<ZeroSlice<T>> for Vec<T> where
T: AsULE + 'static,
{ fn encode_var_ule_as_slices<R>(&self, _: impl FnOnce(&[&[u8]]) -> R) -> R { // unnecessary if the other two are implemented
unreachable!()
}
unsafeimpl<T> EncodeAsVarULE<ZeroSlice<T>> for ZeroVec<'_, T> where
T: AsULE + 'static,
{ fn encode_var_ule_as_slices<R>(&self, _: impl FnOnce(&[&[u8]]) -> R) -> R { // unnecessary if the other two are implemented
unreachable!()
}
unsafeimpl<T, E, F> EncodeAsVarULE<VarZeroSlice<T, F>> for &'_ [E] where
T: VarULE + ?Sized,
E: EncodeAsVarULE<T>,
F: VarZeroVecFormat,
{ fn encode_var_ule_as_slices<R>(&self, _: impl FnOnce(&[&[u8]]) -> R) -> R { // unnecessary if the other two are implemented
unimplemented!()
}
#[allow(clippy::unwrap_used)] // TODO(#1410): Rethink length errors in VZV. fn encode_var_ule_len(&self) -> usize { crate::varzerovec::components::compute_serializable_len::<T, E, F>(self).unwrap() as usize
}
unsafeimpl<T, E, F> EncodeAsVarULE<VarZeroSlice<T, F>> for Vec<E> where
T: VarULE + ?Sized,
E: EncodeAsVarULE<T>,
F: VarZeroVecFormat,
{ fn encode_var_ule_as_slices<R>(&self, _: impl FnOnce(&[&[u8]]) -> R) -> R { // unnecessary if the other two are implemented
unreachable!()
}
unsafeimpl<T, F> EncodeAsVarULE<VarZeroSlice<T, F>> for VarZeroVec<'_, T, F> where
T: VarULE + ?Sized,
F: VarZeroVecFormat,
{ fn encode_var_ule_as_slices<R>(&self, _: impl FnOnce(&[&[u8]]) -> R) -> R { // unnecessary if the other two are implemented
unreachable!()
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.