1

Bindgen 从static constC 风格的全局结构生成它:

pub struct rmw_qos_profile_t {
    pub depth: usize,
    pub deadline: rmw_time_t,
    // ...
}

extern "C" {
    #[link_name = "\u{1}rmw_qos_profile_sensor_data"]
    pub static rmw_qos_profile_sensor_data: rmw_qos_profile_t;
}

我想安全地将它绑定到constRust 中的全局变量。

到目前为止我最好的尝试是

impl From<rmw_qos_profile_t> for QoSProfile {
    fn from(qos_profile: rmw_qos_profile_t) -> Self {
        QoSProfile {
            depth: qos_profile.depth,
            deadline: qos_profile.deadline.into(),
            // ...
        }
    }
}

pub const QOS_PROFILE_SENSOR_DATA: QoSProfile = unsafe { rmw_qos_profile_sensor_data.into() };

我收到以下编译错误:

error[E0013]: constants cannot refer to statics, use a constant instead
   --> /home/deb0ch/ros2_rust_ws/install/rclrs/share/rclrs/rust/src/qos.rs:130:58
    |
130 | pub const QOS_PROFILE_SENSOR_DATA: QoSProfile = unsafe { rmw_qos_profile_sensor_data.into() };
    |                                                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^

error[E0015]: calls in constants are limited to constant functions, tuple structs and tuple variants
   --> /home/deb0ch/ros2_rust_ws/install/rclrs/share/rclrs/rust/src/qos.rs:130:58
    |
130 | pub const QOS_PROFILE_SENSOR_DATA: QoSProfile = unsafe { rmw_qos_profile_sensor_data.into() };
    |                                                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error[E0507]: cannot move out of static item `rmw_qos_profile_sensor_data`
   --> /home/deb0ch/ros2_rust_ws/install/rclrs/share/rclrs/rust/src/qos.rs:130:58
    |
130 | pub const QOS_PROFILE_SENSOR_DATA: QoSProfile = unsafe { rmw_qos_profile_sensor_data.into() };
    |                                                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^ move occurs because `rmw_qos_profile_sensor_data` has type `rcl_bindings::rcl_bindings::rmw_qos_profile_t`, which does not implement the `Copy` trait

我该怎么办?

4

0 回答 0