问题
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