bitvec icon indicating copy to clipboard operation
bitvec copied to clipboard

[Feature request] Add bytemuck Pod and Zeroable traits for bit structs

Open totikom opened this issue 3 years ago • 2 comments
trafficstars

bitvec can be effectively used in protocols for compact storing flags and it sounds reasonable to me, to use bytemuck in the same context. Consider something like this struct for RB-tree:

struct Node<const Ksize: usize, const Vsize: usize> {
    key: [u8; Ksize],
    value: [u8; Vsize],
    size: u8,
    left: Idx,
    right: Idx,
    parent: Idx,
    /// Flag layout:
    ///
    /// 0. is_left_present
    /// 1. is_right_present
    /// 2. is_red
    flags: BitArray<[u8;1]>, // It is just a `u8` in terms of memory footprint
}

Here BitArray allows to pack 3 bools in one u8 and all the possible bit sequences for this type are valid. If you have &[Node<...>] it is obviously safe to cast it to &[u8] and back. And you could do it with just bytemuck::cast_slice(), but to do iit Node should be bytemuck::Pod -> all the fields should be bytemuck::Pod.

Maybe, it is reasonable to create a feature for it, so the users of the crate won't have to compile additional dependency.

I can try to implement it myself, but I'm afraid I need a guidance.

totikom avatar Mar 24 '22 11:03 totikom

We'd be interested in a Zeroize impl as well.

rillian avatar Apr 12 '22 14:04 rillian

We'd be interested in a Zeroize impl as well.

Sorry, zeroize::Zeroize is not ByteMuck::Zeroable. I opened #172 since it's a different dependency.

rillian avatar Apr 12 '22 18:04 rillian