How does a C-style struct with a bitfield get represented in a Rust #[repr(C)] struct?

最后都变了- 提交于 2019-11-28 09:21:19

问题


I have a C struct defined as:

struct my_c_s {
    u_char          *ptr;
    unsigned        flag_a:1;
    unsigned        flag_b:1;
    int             some_num;
}

How would flag_a and flag_b be represented?

#[repr(C)]
pub struct my_rust_s {
    pub ptr: *const u_char,
    //pub flag_a: ?,
    //pub flag_b: ?,
    pub some_num: ::libc::c_int,
}

Can I declare them as bools? Or does that whole thing need to be some sort of set of bits with a single field, and then I bitmask them out?

e.g. pub flag_bits: ::libc::c_uint,


回答1:


No, you can't.

There is an open issue about supporting bitfields, which doesn't seem to be active. In the issue, @retep998 explains how bitfields are handled in winapi. That might be helpful if you need to handle bitfields in C interface.

OP seems to aim at C interoperation, but if you just need bitfields functionality, there are several solutions.

  • You should always consider simple redundant solution: avoid bitfields and let fields align naturally.
  • bitfield, according to the comment -- I didn't know that, but it seems to provide C bitfields equivalent.
  • bitflags. This seems suitable for bit-based flags, which typically represented as enum in C.
  • #[repr(packed)] if you just want to pack fields to some degree, ignoring alignment. The fields will still be aligned to byte boundary.
  • bit-vec if you need homogenious arrays of bits.


来源:https://stackoverflow.com/questions/45229167/how-does-a-c-style-struct-with-a-bitfield-get-represented-in-a-rust-reprc-s

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!