ros2_rust
ros2_rust copied to clipboard
Add dynamic message functionality
This is an overview PR to see the changes at a high level. I recommend you check out this branch and run cargo doc to see what the API looks like. For the review, it will be split into smaller parts.
What are dynamic messages?
Dynamic messages are messages whose type is only known at runtime. You create dynamic messages by specifying the type as a string, e.g. "geometry_msgs/msg/Twist". The dynamic message module then looks for a shared introspection type support library, loads it, and parses the structure of the C message struct from it. That structure contains information about what fields the message contains and what their types are, e.g. field 'angular' of type 'Vector3' at offset 24. Using the structure, the dynamic message module can then read out the value of each field, and even modify it. Basically, it allows accessing the fields of the message as a HashMap from string to a value like this:
enum Value<'msg> {
Float(&'msg f32),
Double(&'msg f64),
...
FloatArray(&'msg [f32]),
...
FloatSequence(&'msg Sequence<f32>),
...
FloatBoundedSequence(DynamicBoundedSequence<'msg, f32>)
..
}
This is useful for writing generic tools such as introspection tools, bridges to other communication systems, or nodes that manipulate messages à la topic_tools.
In C++, a similar thing exists in https://github.com/osrf/dynamic_message_introspection and https://github.com/facontidavide/ros2_introspection.
Features:
- Subscription and publisher for dynamic messages
- Conversion between statically typed and dynamically typed messages
- Safe access of fields – no
unsaferequired in user code
Non-features:
- Dynamic services and clients
- Serde impls for the dynamic type (requires compiling the Rust message package into a dynamic library)
Notes
- The entire
dynamic_messagemodule is behind a feature flag that will be disabled by default. Thus, there is no impact on compile times for the majority of users who do not need this. - I still need to finish writing some more tests
Future work:
- Factor out the common parts of
SubscriptionandDynamicSubscription(and the same for publishers) – this can also help compilation times by monomorphizing Subscription.
@eduidl, you might be interested in this, since I know you wrote a client library for Rust as well as a command-line tool that uses message introspection :)
@nnmm thanks for all the work, it's unfortunate we couldn't merge all the related PRs earlier, but if you ever come back to ros2-rust, we'd happy to review any future work. I'm closing this PR, but feel free to reopen it :slightly_smiling_face: