How do I convert a static global variable generated by Bindgen into a global const?

☆樱花仙子☆ 提交于 2021-01-29 11:54:40

问题


Bindgen generates this from a static const C-style global structure:

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;
}

I would like to safely bind it to a global const in Rust.

My best attempt so far is

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() };

I get the following compilation error:

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

How can I go about it?

来源:https://stackoverflow.com/questions/60748960/how-do-i-convert-a-static-global-variable-generated-by-bindgen-into-a-global-con

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