Skip to content

Zero integer types #2273

Open
Open
@kupiakos

Description

@kupiakos

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)]
enum AlignedZeroU32 {
    #[default]
    Zero = 0,
}

#[repr(C, packed)]
#[derive(Clone, Copy, Default, IntoBytes, FromZeros, Immutable)]
pub struct ZeroU32(AlignedZeroU32);
impl PartialEq for ZeroU32 {
    fn eq(&self, other: &ZeroU32) -> bool { true }
}

impl fmt::Debug for ZeroU32 {
    fn fmt(&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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions