1

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?

deb0ch
  • 1,133
  • 2
  • 16
  • 25
  • 1
    I don't think you can as the C static variable doesn't exist until runtime while the Rust constant needs to be computed at compile time. What problem are you trying to solve by this? – Shepmaster Mar 18 '20 at 23:59
  • The library is defining a bunch of pre-defined profiles as `static const` global structs, what I would lie to achieve is defining this set of profiles from this library, without duplicating variable in Rust. I want changes in default settings upstream to naturally translate in the Rust bindings. – deb0ch Mar 19 '20 at 07:16

0 Answers0