如何将 bindgen 生成的 C 风格枚举转换为另一个枚举?

How do I convert a C-style enum generated by bindgen to another enum?

我在 Rust 中为 C 库创建绑定,Bindgen 生成的枚举如下:

// Rust
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum rmw_qos_history_policy_t {
    RMW_QOS_POLICY_HISTORY_SYSTEM_DEFAULT = 0,
    RMW_QOS_POLICY_HISTORY_KEEP_LAST = 1,
    RMW_QOS_POLICY_HISTORY_KEEP_ALL = 2,
    RMW_QOS_POLICY_HISTORY_UNKNOWN = 3,
}

我需要将它们转换为:

// Rust
pub enum QoSHistoryPolicy {
    SystemDefault = 0,
    KeepLast = 1,
    KeepAll = 2,
    Unknown = 3,
}

从该 C 库导入常量值时:

// C library
const rmw_qos_history_policy_t some_value_from_C = RMW_QOS_POLICY_HISTORY_SYSTEM_DEFAULT;

我想做这样的事情:

let some_value: QoSHistoryPolicy = some_value_from_C;

我该怎么做?

它看起来很适合 QoSHistoryPolicy From trait

impl From<rmw_qos_history_policy_t> for QoSHistoryPolicy {
    fn from(raw: rmw_qos_history_policy_t) -> Self {
        match raw {
            rmw_qos_history_policy_t::RMW_QOS_POLICY_HISTORY_SYSTEM_DEFAULT => QoSHistoryPolicy::SystemDefault,
            rmw_qos_history_policy_t::RMW_QOS_POLICY_HISTORY_KEEP_LAST => QoSHistoryPolicy::KeepLast,
            rmw_qos_history_policy_t::RMW_QOS_POLICY_HISTORY_KEEP_ALL => QoSHistoryPolicy::KeepAll,
            rmw_qos_history_policy_t::RMW_QOS_POLICY_HISTORY_UNKNOWN => QoSHistoryPolicy::Unknown
        }
    }
}

现在应该可以了

let some_value: QoSHistoryPolicy = some_value_from_C.into();

编译器不检查枚举的 ABI 兼容性,因此不提供在这些类型之间转换值的直接方法。以下是一些可能的解决方案。

1。一对一匹配

尽管会导致详尽的代码,但这是微不足道且安全的。

impl From<rmw_qos_history_policy_t> for QoSHistoryPolicy {
    fn from(x: rmw_qos_history_policy_t) -> Self {
        use rmw_qos_history_policy_t::*;
        match x {
            RMW_QOS_POLICY_HISTORY_SYSTEM_DEFAULT => QoSHistoryPolicy::SystemDefault,
            RMW_QOS_POLICY_HISTORY_KEEP_LAST => QoSHistoryPolicy::KeepLast,
            RMW_QOS_POLICY_HISTORY_KEEP_ALL => QoSHistoryPolicy::KeepAll,
            RMW_QOS_POLICY_HISTORY_UNKNOWN => QoSHistoryPolicy::Unknown,
        }
    }
}

2。铸造 + FromPrimitive

Rust 允许您使用 as 运算符将无字段枚举转换为整数类型。然而,相反的转换并不总是安全的。使用 num 箱子导出 FromPrimitive 以获得丢失的部分。

#[derive(FromPrimitive)]
pub enum QoSHistoryPolicy { ... }

impl From<rmw_qos_history_policy_t> for QoSHistoryPolicy {
    fn from(x: rmw_qos_history_policy_t) -> Self {
        FromPrimitive::from_u32(x as _).expect("1:1 enum variant matching, all good")
    }
}

3。需要枚举?

如果您只想对低级绑定进行抽象,则可能不需要新的枚举类型。

#[repr(transparent)]
pub struct QoSHistoryPolicy(rmw_qos_history_policy_t);

上面的类型包含相同的信息和二进制表示,但可以公开封装的 API。从低级类型到高级类型的转换变得微不足道。主要的缺点是你失去了与其变体的模式匹配。

4。你一个人

绝对确定两个枚举的二进制表示形式相同时,您可以transmute它们之间。编译器不会在这里帮助你,这远非推荐。

unsafe {
    let policy: QoSHistoryPolicy = std::mem::transmute(val);
}

另请参阅:

  • How do I match enum values with an integer?