2

我在 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;

我该怎么办?

4

2 回答 2

7

编译器不检查枚举的 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。然而,相反的转换并不总是安全的。FromPrimitive使用板条箱派生num以获得丢失的部分。

#[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);
}

也可以看看:

于 2020-03-18T11:50:14.063 回答
3

From 它看起来QoSHistoryPolicy很适合.

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();
于 2020-03-18T11:39:33.260 回答