The ros2_rust project strives to maintain consistency with the upstream ROS 2 message generation
system. To this end, it provides two main packages: rosidl_generator_rs and rosidl_runtime_rs.
These packages provide the infrastructure required for defining and using ROS 2 messages, services,
and actions in Rust.
At a high level, the rosidl_generator_rs package handles generation of interface crates from the
.msg, .srv, and .action files defined by a user. The rosidl_runtime_rs package provides
common functionality shared across message packages, such as support types and traits. Each of these
packages is described in more detail below.
rosidl_generator_rs follows a very similar pattern to the other message generation packages for
ROS 2. To tie into this pipeline, it registers itself as a "rosidl_generate_idl_interfaces"
extension with ament. Doing so ensures that message packages calling rosidl_generate_interfaces
will invoke the Rust language generator in addition to any others. This is accomplished using the
various CMake scripts under the cmake directory. When this happens, the input interface files will
be converted into IDL files which, along with additional metadata, are fed into the generate_rs
function of rosidl_generator_rs/__init__.py.
From here, the IDL files are parsed into an internal representation using the upstream
rosidl_parser package. This abstract
representation is then used to instantiate the various template files under the resource
subdirectory, producing a full Rust crate for each package.
For each input message type, two structs are generated:
- An ergonomic representation of the message, using more idiomatic
stdtypes likeVecandStringfor sequence and string fields. These are placed directly in themsgsubmodule of the crate. - A FFI-suitable
structthat is ABI-compatible with the layout expected by the RMW layer. While less ergonomic, these avoid the conversion overhead when passed to RMW. Thesestructs are placed under themsg::rmwsubmodule.
All the produced structs implement the standard traits from std when possible, such as Clone,
PartialEq, and Debug. Additionally, when the generated crate's serde feature is enabled, these
structs implement the Serialize and Deserialize traits.
rosidl_runtime_rs is a runtime support package, providing Message, Service, and Action
traits that are implemented by the corresponding structs generated by rosidl_generator_rs. These
allow for generic interaction with these various interface types, both in client libraries like
rclrs and in user code.
The package also provides a number of string and sequence types that are ABI-compatible with their
rosidl_runtime_c equivalents. This allows for more ergonomic use of the RMW-native message types.