File tree Expand file tree Collapse file tree 3 files changed +63
-0
lines changed Expand file tree Collapse file tree 3 files changed +63
-0
lines changed Original file line number Diff line number Diff line change
1
+ use r2r:: { WrappedNativeMsgUntyped , WrappedTypesupport } ;
2
+
3
+ fn main ( ) -> Result < ( ) , Box < dyn std:: error:: Error > > {
4
+ let msg = r2r:: std_msgs:: msg:: String {
5
+ data : "Hello, world" . into ( ) ,
6
+ } ;
7
+
8
+ let bytes = msg. to_serialized_bytes ( ) ?;
9
+
10
+ // bytes to "untyped" r2r msg
11
+ let mut native = WrappedNativeMsgUntyped :: new_from ( "std_msgs/msg/String" ) ?;
12
+ native. from_serialized_bytes ( & bytes) ?;
13
+
14
+ // "untyped" msg to json
15
+ let json = native. to_json ( ) ?;
16
+
17
+ println ! ( "as json\n ----\n {}" , serde_json:: to_string_pretty( & json) ?) ;
18
+
19
+ // bytes to r2r msg.
20
+ let msg2 = r2r:: std_msgs:: msg:: String :: from_serialized_bytes ( & bytes) ?;
21
+
22
+ println ! ( "as r2r msg\n ----\n {:#?}" , msg2) ;
23
+
24
+ // json to r2r msg
25
+ let msg3: r2r:: std_msgs:: msg:: String = serde_json:: from_value ( json) ?;
26
+
27
+ assert_eq ! ( msg, msg2) ;
28
+ assert_eq ! ( msg2, msg3) ;
29
+
30
+ Ok ( ( ) )
31
+ }
Original file line number Diff line number Diff line change @@ -81,6 +81,7 @@ pub use msg_types::WrappedActionTypeSupport;
81
81
pub use msg_types:: WrappedNativeMsg as NativeMsg ;
82
82
pub use msg_types:: WrappedServiceTypeSupport ;
83
83
pub use msg_types:: WrappedTypesupport ;
84
+ pub use msg_types:: WrappedNativeMsgUntyped ;
84
85
85
86
mod utils;
86
87
pub use utils:: * ;
Original file line number Diff line number Diff line change @@ -406,6 +406,37 @@ impl WrappedNativeMsgUntyped {
406
406
}
407
407
}
408
408
409
+ pub fn from_serialized_bytes ( & mut self , data : & [ u8 ] ) -> Result < ( ) > {
410
+ // TODO: Copy paste from above, should refactor later.
411
+ use r2r_rcl:: * ;
412
+
413
+ let msg_buf = rcl_serialized_message_t {
414
+ buffer : data. as_ptr ( ) as * mut u8 ,
415
+ buffer_length : data. len ( ) ,
416
+ buffer_capacity : data. len ( ) ,
417
+
418
+ // Since its read only, this should never be used ..
419
+ allocator : unsafe { rcutils_get_default_allocator ( ) } ,
420
+ } ;
421
+
422
+ // Note From the docs of rmw_deserialize, its not clear whether this reuses
423
+ // any part of msg_buf. However it shouldn't matter since from_native
424
+ // clones everything again anyway ..
425
+ let result = unsafe {
426
+ rmw_deserialize (
427
+ & msg_buf as * const rcl_serialized_message_t ,
428
+ self . ts ,
429
+ self . msg ,
430
+ )
431
+ } ;
432
+
433
+ if result == RCL_RET_OK as i32 {
434
+ Ok ( ( ) )
435
+ } else {
436
+ Err ( Error :: from_rcl_error ( result) )
437
+ }
438
+ }
439
+
409
440
pub fn to_json ( & self ) -> Result < serde_json:: Value > {
410
441
let json = ( self . msg_to_json ) ( self . msg ) ;
411
442
json. map_err ( |serde_err| Error :: SerdeError {
You can’t perform that action at this time.
0 commit comments