【问题标题】:Struct with mixed bitflag and normal members具有混合位标志和普通成员的结构
【发布时间】:2018-08-14 20:45:35
【问题描述】:

我正在尝试在 Rust for FFI 中重新创建一个包含混合位域成员和“普通”成员的 C 结构。

我已经读到bitflags crate 将是首选,但不幸的是,我发现文档缺乏关于语法实际工作原理的说明。

bitflags crate 可以更轻松地创建与 C 中使用枚举类似的样式的位掩码。 bitfield crate 声称创建可以访问的位域,但是我不知道它是如何工作的。

我有一个这样的 C 结构:

struct mixed {
    unsigned int flag_1_1 : 1;
    unsigned int flag_2_7 : 7;
    unsigned int flag_3_8 : 8;

    unsigned int some_val1;
    unsigned int some_val2;

    unsigned int flag_4_16 : 16;
};

我不知道如何在 Rust 中表示它,我会使用板条箱 libc 来访问 c_uint,但除此之外,我目前几乎没有想法并找到其他代码这样做并没有被证明是成功的:

#[repr(transparent)] // do I also need repr(C) ?
struct mixed {
    flags_1_3: mixed_flags_1_3;
    some_val1: c_uint;
    some_val2: c_uint;
    flags_4: mixed_flags_4;
}

bitfield!(
    #[repr(transparent)] // do I also need repr(C), here too?
    struct mixed_flags_1_3(u16 /* what about this parameter? */) {
        u8; // what does this mean?
        /* get/field, set: first bit, last bit; */
        flag_1_1, _: 0, 0;
        flag_2_7, _: 7, 1;
        flag_3_8, _: 15, 8;
    }
)

bitfield!(
    #[repr(transparent)]
    struct mixed_flags_4(u8) {
        u8;
        flag_4_16, _: 15, 0;
    }
)

这些只是猜测,如何创建正确的表示?

【问题讨论】:

  • 你不想要 bitflags,你想要 bit fields。它们不是一回事。
  • 哦,我对链接 bitflags 板条箱的答案感到困惑。现在这是有道理的。但是没有关于创建的位标志是否兼容的文档。由于是特定于编译器的,这可能是不可能的,所以我不得不用 C 编写访问器函数。

标签: struct rust ffi bit-fields bitflags


【解决方案1】:

使用rustc -- -Z unstable-options --pretty=expanded 我想我可以发现宏确实如此,这似乎产生了一些正确的东西,但是这可能仅在编译器不尝试填充或重新排序结构中的位域时才兼容.

#[repr(transparent)] // do I also need repr(C) ?
struct mixed {
    flags_1_3: mixed_flags_1_3;
    some_val1: c_uint;
    some_val2: c_uint;
    flags_4: mixed_flags_4;
}

bitfield!(
    #[repr(transparent)] // do I also need repr(C), here too?
    //  Results in a "tuple struct", ie. u16 = total size of bitfields
    struct mixed_flags_1_3(u16) {
        // All the following fields value should be treated as an
        // unsigned int when accessed
        c_uint;
        /* get/field, set: first bit, last bit; */
        flag_1_1, _: 0, 0;
        flag_2_7, _: 7, 1;
        // One could change the type again here, if one wanted to:
        // u16
        flag_3_8, _: 15, 8;
    }
)

bitfield!(
    #[repr(transparent)]
    struct mixed_flags_4(u16) {
        c_uint;
        flag_4_16, _: 15, 0;
    }
)

但至少现在我认为我只会使用 libclang 和 bindgen 作为依赖项并自动生成我的绑定,因为上述平台兼容性问题。

【讨论】:

    【解决方案2】:

    在这种情况下,您可以查看bindgen 生成的代码:

    $ bindgen test.h
    
    #[repr(C)]
    #[derive(Copy, Clone, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
    pub struct __BindgenBitfieldUnit<Storage, Align>
    where
        Storage: AsRef<[u8]> + AsMut<[u8]>,
    {
        storage: Storage,
        align: [Align; 0],
    }
    
    //skipped code with access method for bit fields 
    
    #[repr(C)]
    #[derive(Debug, Copy, Clone)]
    pub struct mixed {
        pub _bitfield_1: __BindgenBitfieldUnit<[u8; 2usize], u8>,
        pub some_val1: ::std::os::raw::c_uint,
        pub some_val2: ::std::os::raw::c_uint,
        pub _bitfield_2: __BindgenBitfieldUnit<[u8; 2usize], u16>,
        pub __bindgen_padding_0: u16,
    }
    

    【讨论】:

    • 然而,C 标准并没有保证你对布局一无所知,所以 bindgen 创建的绑定只适用于你当前开发的平台。我想知道位域宏是否会处理这个问题,但我实际上根本无法理解它应该做什么,文档似乎有点缺乏。我刚刚在我的 crate 中使用了 bingen,这是另一个附加的依赖项,否则很难保持便携性。
    猜你喜欢
    • 1970-01-01
    • 2017-10-25
    • 2020-04-20
    • 2012-03-02
    • 2011-08-29
    • 2012-08-07
    • 1970-01-01
    • 1970-01-01
    • 2014-01-12
    相关资源
    最近更新 更多