Skip to content

Commit 2afa062

Browse files
authored
Vendorize rosgraph msgs (#345)
* Revert "Version 0.4.0 (#343)" This reverts commit e7065a7. * Vendorize rosgraph_msgs Signed-off-by: Esteve Fernandez <[email protected]> * Fix module path * Fix format --------- Signed-off-by: Esteve Fernandez <[email protected]>
1 parent 5014fcd commit 2afa062

File tree

6 files changed

+144
-7
lines changed

6 files changed

+144
-7
lines changed

rclrs/Cargo.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ ament_rs = { version = "0.2", optional = true }
2020
futures = "0.3"
2121
# Needed for dynamic messages
2222
libloading = { version = "0.8", optional = true }
23-
# Needed for /clock topic subscription when using simulation time
24-
rosgraph_msgs = "*"
2523
# Needed for the Message trait, among others
2624
rosidl_runtime_rs = "0.3"
2725

rclrs/src/time_source.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::clock::{Clock, ClockSource, ClockType};
2+
use crate::vendor::rosgraph_msgs::msg::Clock as ClockMsg;
23
use crate::{MandatoryParameter, Node, QoSProfile, Subscription, QOS_PROFILE_CLOCK};
3-
use rosgraph_msgs::msg::Clock as ClockMsg;
44
use std::sync::{Arc, Mutex, RwLock, Weak};
55

66
/// Time source for a node that drives the attached clock.

rclrs/src/vendor/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44

55
pub mod builtin_interfaces;
66
pub mod rcl_interfaces;
7+
pub mod rosgraph_msgs;

rclrs/src/vendor/rosgraph_msgs/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#![allow(non_camel_case_types)]
2+
3+
pub mod msg;

rclrs/src/vendor/rosgraph_msgs/msg.rs

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
pub mod rmw {
2+
#[cfg(feature = "serde")]
3+
use serde::{Deserialize, Serialize};
4+
5+
#[link(name = "rosgraph_msgs__rosidl_typesupport_c")]
6+
extern "C" {
7+
fn rosidl_typesupport_c__get_message_type_support_handle__rosgraph_msgs__msg__Clock(
8+
) -> *const std::os::raw::c_void;
9+
}
10+
11+
#[link(name = "rosgraph_msgs__rosidl_generator_c")]
12+
extern "C" {
13+
fn rosgraph_msgs__msg__Clock__init(msg: *mut Clock) -> bool;
14+
fn rosgraph_msgs__msg__Clock__Sequence__init(
15+
seq: *mut rosidl_runtime_rs::Sequence<Clock>,
16+
size: usize,
17+
) -> bool;
18+
fn rosgraph_msgs__msg__Clock__Sequence__fini(seq: *mut rosidl_runtime_rs::Sequence<Clock>);
19+
fn rosgraph_msgs__msg__Clock__Sequence__copy(
20+
in_seq: &rosidl_runtime_rs::Sequence<Clock>,
21+
out_seq: *mut rosidl_runtime_rs::Sequence<Clock>,
22+
) -> bool;
23+
}
24+
25+
// Corresponds to rosgraph_msgs__msg__Clock
26+
#[repr(C)]
27+
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
28+
#[derive(Clone, Debug, PartialEq, PartialOrd)]
29+
pub struct Clock {
30+
pub clock: crate::vendor::builtin_interfaces::msg::rmw::Time,
31+
}
32+
33+
impl Default for Clock {
34+
fn default() -> Self {
35+
unsafe {
36+
let mut msg = std::mem::zeroed();
37+
if !rosgraph_msgs__msg__Clock__init(&mut msg as *mut _) {
38+
panic!("Call to rosgraph_msgs__msg__Clock__init() failed");
39+
}
40+
msg
41+
}
42+
}
43+
}
44+
45+
impl rosidl_runtime_rs::SequenceAlloc for Clock {
46+
fn sequence_init(seq: &mut rosidl_runtime_rs::Sequence<Self>, size: usize) -> bool {
47+
// SAFETY: This is safe since the pointer is guaranteed to be valid/initialized.
48+
unsafe { rosgraph_msgs__msg__Clock__Sequence__init(seq as *mut _, size) }
49+
}
50+
fn sequence_fini(seq: &mut rosidl_runtime_rs::Sequence<Self>) {
51+
// SAFETY: This is safe since the pointer is guaranteed to be valid/initialized.
52+
unsafe { rosgraph_msgs__msg__Clock__Sequence__fini(seq as *mut _) }
53+
}
54+
fn sequence_copy(
55+
in_seq: &rosidl_runtime_rs::Sequence<Self>,
56+
out_seq: &mut rosidl_runtime_rs::Sequence<Self>,
57+
) -> bool {
58+
// SAFETY: This is safe since the pointer is guaranteed to be valid/initialized.
59+
unsafe { rosgraph_msgs__msg__Clock__Sequence__copy(in_seq, out_seq as *mut _) }
60+
}
61+
}
62+
63+
impl rosidl_runtime_rs::Message for Clock {
64+
type RmwMsg = Self;
65+
fn into_rmw_message(
66+
msg_cow: std::borrow::Cow<'_, Self>,
67+
) -> std::borrow::Cow<'_, Self::RmwMsg> {
68+
msg_cow
69+
}
70+
fn from_rmw_message(msg: Self::RmwMsg) -> Self {
71+
msg
72+
}
73+
}
74+
75+
impl rosidl_runtime_rs::RmwMessage for Clock
76+
where
77+
Self: Sized,
78+
{
79+
const TYPE_NAME: &'static str = "rosgraph_msgs/msg/Clock";
80+
fn get_type_support() -> *const std::os::raw::c_void {
81+
// SAFETY: No preconditions for this function.
82+
unsafe {
83+
rosidl_typesupport_c__get_message_type_support_handle__rosgraph_msgs__msg__Clock()
84+
}
85+
}
86+
}
87+
} // mod rmw
88+
89+
#[cfg(feature = "serde")]
90+
use serde::{Deserialize, Serialize};
91+
92+
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
93+
#[derive(Clone, Debug, PartialEq, PartialOrd)]
94+
pub struct Clock {
95+
pub clock: crate::vendor::builtin_interfaces::msg::Time,
96+
}
97+
98+
impl Default for Clock {
99+
fn default() -> Self {
100+
<Self as rosidl_runtime_rs::Message>::from_rmw_message(
101+
crate::vendor::rosgraph_msgs::msg::rmw::Clock::default(),
102+
)
103+
}
104+
}
105+
106+
impl rosidl_runtime_rs::Message for Clock {
107+
type RmwMsg = crate::vendor::rosgraph_msgs::msg::rmw::Clock;
108+
109+
fn into_rmw_message(msg_cow: std::borrow::Cow<'_, Self>) -> std::borrow::Cow<'_, Self::RmwMsg> {
110+
match msg_cow {
111+
std::borrow::Cow::Owned(msg) => std::borrow::Cow::Owned(Self::RmwMsg {
112+
clock: crate::vendor::builtin_interfaces::msg::Time::into_rmw_message(
113+
std::borrow::Cow::Owned(msg.clock),
114+
)
115+
.into_owned(),
116+
}),
117+
std::borrow::Cow::Borrowed(msg) => std::borrow::Cow::Owned(Self::RmwMsg {
118+
clock: crate::vendor::builtin_interfaces::msg::Time::into_rmw_message(
119+
std::borrow::Cow::Borrowed(&msg.clock),
120+
)
121+
.into_owned(),
122+
}),
123+
}
124+
}
125+
126+
fn from_rmw_message(msg: Self::RmwMsg) -> Self {
127+
Self {
128+
clock: crate::vendor::builtin_interfaces::msg::Time::from_rmw_message(msg.clock),
129+
}
130+
}
131+
}

rclrs/vendor_interfaces.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# This script produces the `vendor` module inside `rclrs` by copying
2-
# the generated code for the `rcl_interfaces` package and its dependency
3-
# `builtin_interfaces` and adjusting the submodule paths in the code.
2+
# the generated code for the `rosgraph_msgs` and `rcl_interfaces` packages and
3+
# its dependency `builtin_interfaces` and adjusting the submodule paths in the
4+
# code.
45
# If these packages, or the `rosidl_generator_rs`, get changed, you can
56
# update the `vendor` module by running this script.
67
# The purpose is to avoid an external dependency on `rcl_interfaces`, which
@@ -12,14 +13,15 @@
1213
import subprocess
1314

1415
def get_args():
15-
parser = argparse.ArgumentParser(description='Vendor the rcl_interfaces and builtin_interfaces packages into rclrs')
16+
parser = argparse.ArgumentParser(description='Vendor the rcl_interfaces, builtin_interfaces and rosgraph_msgs packages into rclrs')
1617
parser.add_argument('install_base', metavar='install_base', type=Path,
1718
help='the install base (must have non-merged layout)')
1819
return parser.parse_args()
1920

2021
def adjust(pkg, text):
2122
text = text.replace('builtin_interfaces::', 'crate::vendor::builtin_interfaces::')
2223
text = text.replace('rcl_interfaces::', 'crate::vendor::rcl_interfaces::')
24+
text = text.replace('rosgraph_msgs::', 'crate::vendor::rosgraph_msgs::')
2325
text = text.replace('crate::msg', f'crate::vendor::{pkg}::msg')
2426
text = text.replace('crate::srv', f'crate::vendor::{pkg}::srv')
2527
return text
@@ -34,18 +36,20 @@ def copy_adjusted(pkg, src, dst):
3436
3537
pub mod builtin_interfaces;
3638
pub mod rcl_interfaces;
39+
pub mod rosgraph_msgs;
3740
""".format(Path(__file__).name)
3841

3942
def main():
4043
args = get_args()
4144
assert args.install_base.is_dir(), "Install base does not exist"
4245
assert (args.install_base / 'builtin_interfaces').is_dir(), "Install base does not contain builtin_interfaces"
4346
assert (args.install_base / 'rcl_interfaces').is_dir(), "Install base does not contain rcl_interfaces"
47+
assert (args.install_base / 'rosgraph_msgs').is_dir(), "Install base does not contain rosgraph_msgs"
4448
rclrs_root = Path(__file__).parent
4549
vendor_dir = rclrs_root / 'src' / 'vendor'
4650
if vendor_dir.exists():
4751
shutil.rmtree(vendor_dir)
48-
for pkg in ['builtin_interfaces', 'rcl_interfaces']:
52+
for pkg in ['builtin_interfaces', 'rcl_interfaces', 'rosgraph_msgs']:
4953
src = args.install_base / pkg / 'share' / pkg / 'rust' / 'src'
5054
dst = vendor_dir / pkg
5155
dst.mkdir(parents=True)

0 commit comments

Comments
 (0)