qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [RFC 06/13] rust: add bindings for memattrs


From: Paolo Bonzini
Subject: Re: [RFC 06/13] rust: add bindings for memattrs
Date: Mon, 20 Jan 2025 18:19:25 +0100
User-agent: Mozilla Thunderbird

On 1/20/25 17:52, Zhao Liu wrote:
Sorry I missed this comment before...

Now I have a MemTxAttrs like,

typedef struct MemTxAttrs {
     unsigned int secure:1;
     unsigned int space:2;
     unsigned int user:1;
     unsigned int memory:1;
     unsigned int requester_id:16;
     unsigned int pid:8;
     bool unspecified;
     uint8_t _reserved1;
     uint16_t _reserved2;
} MemTxAttrs;

and its binding is,

#[repr(C)]
#[repr(align(4))]
#[derive(Debug, Default, Copy, Clone)]
pub struct MemTxAttrs {
     pub _bitfield_align_1: [u16; 0],
     pub _bitfield_1: __BindgenBitfieldUnit<[u8; 4usize]>,
     pub unspecified: bool,
     pub _reserved1: u8,
     pub _reserved2: u16,
}

unfortunately, Zeroable can't be applied to __BindgenBitfieldUnit since
event its member (`storage`) is private :-(.

But there's a solution to force (and at the same time unsafely) ZERO the
entire structure in const:

  * const_zero macro: https://docs.rs/const-zero/latest/const_zero/

With const_zero, we can implement Zeroable for MemTxAttrs:

unsafe impl Zeroable for MemTxAttrs {
     const ZERO: Self = unsafe {const_zero!(MemTxAttrs)};
}

Another solution would be to implement Zeroable for __BindgenBitfieldUnit in bindings.rs, but this is much nicer! It works even with old Rust versions and, even though it needs manual implementation of the trait each type, it doesn't require enumerating the fields one by one. So it's better than the current version of Zeroable and, if you wish, you can also replace existing implementations of Zeroable with const_zero.

I wouldn't bother adding a subproject; just include the macro in zeroable.rs, with attribution and MIT license, and document that it might go away once we require a newer rustc.

Thanks very much!

Paolo




reply via email to

[Prev in Thread] Current Thread [Next in Thread]