Skip to content

Support AsBytes for fields with a size set as a generic constant #1182

Description

@saltatory

Use Case

I am working on a high-throughput IO project reading/writing fixed size blocks to disk. I have a broadly used compilation parameter for the block id size.

I have a struct like the following:

#[derive(FromBytes, FromZeroes, AsBytes, Unaligned)]
#[repr(packed)]
pub struct IndexEntryFlags(u8);

bitflags! {
    impl IndexEntryFlags: u8 {
        const NONE = 0b00;
        const DELETE = 0b01;
    }
}

#[derive(FromBytes, FromZeroes, AsBytes, Unaligned)]
#[repr(packed)]
pub struct IndexEntry<const SIZE_BLOCK_ID: usize> {
    block_number: U64,
    flags: IndexEntryFlags,
    block_id: [u8; SIZE_BLOCK_ID] 
}

Problem

This generates an error:

error[E0793]: reference to packed field is unaligned

... even though the exact same compilation works with:

...
   block_id: [u8; 32]
...

Request

Support generic constants in fixed size arrays for AsBytes.

Metadata

Metadata

Assignees

No one assigned

    Labels

    blocking-next-releaseThis issue should be resolved before we release on crates.iobugSomething isn't working

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions