14
14
use std:: borrow:: Cow ;
15
15
16
16
use clap:: Parser ;
17
+ #[ cfg( all( feature = "shared-memory" , feature = "unstable" ) ) ]
18
+ use zenoh:: shm:: { zshm, zshmmut} ;
17
19
use zenoh:: { bytes:: ZBytes , config:: Config , key_expr:: KeyExpr } ;
18
20
use zenoh_examples:: CommonArgs ;
19
21
@@ -31,43 +33,25 @@ async fn main() {
31
33
let subscriber = session. declare_subscriber ( & key_expr) . await . unwrap ( ) ;
32
34
33
35
println ! ( "Press CTRL-C to quit..." ) ;
34
- while let Ok ( sample) = subscriber. recv_async ( ) . await {
36
+ while let Ok ( mut sample) = subscriber. recv_async ( ) . await {
37
+ let kind = sample. kind ( ) ;
38
+ let key_str = sample. key_expr ( ) . as_str ( ) . to_owned ( ) ;
39
+
35
40
// Print overall payload information
36
- let ( payload_type, payload) = handle_bytes ( sample. payload ( ) ) ;
41
+ let ( payload_type, payload) = handle_bytes ( sample. payload_mut ( ) ) ;
37
42
print ! (
38
43
">> [Subscriber] Received {} ('{}': '{}') [{}] " ,
39
- sample. kind( ) ,
40
- sample. key_expr( ) . as_str( ) ,
41
- payload,
42
- payload_type,
44
+ kind, key_str, payload, payload_type,
43
45
) ;
44
46
45
47
// Print attachment information
46
- if let Some ( att) = sample. attachment ( ) {
48
+ if let Some ( att) = sample. attachment_mut ( ) {
47
49
let ( attachment_type, attachment) = handle_bytes ( att) ;
48
50
print ! ( " ({}: {})" , attachment_type, attachment) ;
49
51
}
50
52
51
53
println ! ( ) ;
52
54
}
53
-
54
- // // Try to get a mutable reference to the SHM buffer. If this subscriber is the only subscriber
55
- // // holding a reference to the SHM buffer, then it will be able to get a mutable reference to it.
56
- // // With the mutable reference at hand, it's possible to mutate in place the SHM buffer content.
57
-
58
- // while let Ok(mut sample) = subscriber.recv_async().await {
59
- // let kind = sample.kind();
60
- // let key_expr = sample.key_expr().to_string();
61
- // match sample.payload_mut().as_shm_mut() {
62
- // Ok(payload) => println!(
63
- // ">> [Subscriber] Received {} ('{}': '{:02x?}')",
64
- // kind, key_expr, payload
65
- // ),
66
- // Err(e) => {
67
- // println!(">> [Subscriber] Not a ShmBufInner: {:?}", e);
68
- // }
69
- // }
70
- // }
71
55
}
72
56
73
57
#[ derive( clap:: Parser , Clone , PartialEq , Eq , Hash , Debug ) ]
@@ -84,7 +68,7 @@ fn parse_args() -> (Config, KeyExpr<'static>) {
84
68
( args. common . into ( ) , args. key )
85
69
}
86
70
87
- fn handle_bytes ( bytes : & ZBytes ) -> ( & str , Cow < str > ) {
71
+ fn handle_bytes ( bytes : & mut ZBytes ) -> ( & str , Cow < str > ) {
88
72
// Determine buffer type for indication purpose
89
73
let bytes_type = {
90
74
// if Zenoh is built without SHM support, the only buffer type it can receive is RAW
@@ -100,10 +84,14 @@ fn handle_bytes(bytes: &ZBytes) -> (&str, Cow<str>) {
100
84
"UNKNOWN"
101
85
}
102
86
103
- // if Zenoh is built with SHM support and with SHM API we can detect the exact buffer type
87
+ // if Zenoh is built with SHM support and with SHM API we can detect the exact buffer type
104
88
#[ cfg( all( feature = "shared-memory" , feature = "unstable" ) ) ]
105
- match bytes. as_shm ( ) {
106
- Some ( _) => "SHM" ,
89
+ match bytes. as_shm_mut ( ) {
90
+ // try to mutate SHM buffer to get it's mutability property
91
+ Some ( shm) => match <& mut zshm as TryInto < & mut zshmmut > >:: try_into ( shm) {
92
+ Ok ( _shm_mut) => "SHM (MUT)" ,
93
+ Err ( _) => "SHM (IMMUT)" ,
94
+ } ,
107
95
None => "RAW" ,
108
96
}
109
97
} ;
0 commit comments