You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We've implemented some protocols that are designed to require all-zeroes in segments of the message. We ended up making this statically required by defining wrapper types around an enum. Example for u32:
#[derive(Clone,Copy,Default,IntoBytes,Immutable,FromZeros)]#[repr(u32)]enumAlignedZeroU32{#[default]Zero = 0,}#[repr(C, packed)]#[derive(Clone,Copy,Default,IntoBytes,FromZeros,Immutable)]pubstructZeroU32(AlignedZeroU32);implPartialEqforZeroU32{fneq(&self,other:&ZeroU32) -> bool{true}}impl fmt::DebugforZeroU32{fnfmt(&self,fmt:&mut fmt::Formatter<'_>) -> fmt::Result{// No need to debug the inner field.
fmt.debug_struct("ZeroU32").finish()}}// PartialEq against u32 with `== 0`, U32<T: zerocopy::byte_order::ByteOrder> with `== U32::ZERO`
There were enough hurdles to jump through to implement this correctly that it'd be convenient to have these as types within zerocopy. This doesn't use zerocopy::Unalign<AlignedZeroU32> because PartialEq and Debug require manual implementations
The text was updated successfully, but these errors were encountered:
I definitely would like to see zerocopy support something like this (see also: #31), especially if we can support infallible &[ZeroU8] -> &T conversions where T: FromZeros, as some users have memory they know to be zeroed (e.g. pages allocated via mmap etc).
I'll give some thought to how this would best be represented in our API, but I'm broadly in support.
Ah, sorry I didn't see the first issue; that is similar.
Hmm, is &[ZeroU8] -> &T where T: FromZeros + Unaligned a practical operation? It yields a span of zeroed memory, but without the pointer provenance to soundly mutate that memory (e.g. if passed to FFI). &mut [ZeroU8] -> &mut T is unsound. Box<ZeroU8> -> Box<T> is sound iff T: FromZeros + Unaligned (extend to non-1 align as appropriate). #1183 is the best solution, so long as 3P types can unsafely indicate that consuming self removes all invariants in the source type (i.e. it is an exclusively-owned-to-owned conversion), so an MMIO library can convert MmioBox<[u8]> to MmioBox<T>.
We've implemented some protocols that are designed to require all-zeroes in segments of the message. We ended up making this statically required by defining wrapper types around an
enum
. Example foru32
:There were enough hurdles to jump through to implement this correctly that it'd be convenient to have these as types within
zerocopy
. This doesn't usezerocopy::Unalign<AlignedZeroU32>
becausePartialEq
andDebug
require manual implementationsThe text was updated successfully, but these errors were encountered: