Skip to content

Commit a6bc509

Browse files
authored
Shm mutation example (#1672)
Add SHM buffer mutation functionality to examples Add payload_mut accessor to ReplyError
1 parent f3f0924 commit a6bc509

File tree

2 files changed

+23
-29
lines changed

2 files changed

+23
-29
lines changed

examples/examples/z_sub_shm.rs

+17-29
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
use std::borrow::Cow;
1515

1616
use clap::Parser;
17+
#[cfg(all(feature = "shared-memory", feature = "unstable"))]
18+
use zenoh::shm::{zshm, zshmmut};
1719
use zenoh::{bytes::ZBytes, config::Config, key_expr::KeyExpr};
1820
use zenoh_examples::CommonArgs;
1921

@@ -31,43 +33,25 @@ async fn main() {
3133
let subscriber = session.declare_subscriber(&key_expr).await.unwrap();
3234

3335
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+
3540
// Print overall payload information
36-
let (payload_type, payload) = handle_bytes(sample.payload());
41+
let (payload_type, payload) = handle_bytes(sample.payload_mut());
3742
print!(
3843
">> [Subscriber] Received {} ('{}': '{}') [{}] ",
39-
sample.kind(),
40-
sample.key_expr().as_str(),
41-
payload,
42-
payload_type,
44+
kind, key_str, payload, payload_type,
4345
);
4446

4547
// Print attachment information
46-
if let Some(att) = sample.attachment() {
48+
if let Some(att) = sample.attachment_mut() {
4749
let (attachment_type, attachment) = handle_bytes(att);
4850
print!(" ({}: {})", attachment_type, attachment);
4951
}
5052

5153
println!();
5254
}
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-
// }
7155
}
7256

7357
#[derive(clap::Parser, Clone, PartialEq, Eq, Hash, Debug)]
@@ -84,7 +68,7 @@ fn parse_args() -> (Config, KeyExpr<'static>) {
8468
(args.common.into(), args.key)
8569
}
8670

87-
fn handle_bytes(bytes: &ZBytes) -> (&str, Cow<str>) {
71+
fn handle_bytes(bytes: &mut ZBytes) -> (&str, Cow<str>) {
8872
// Determine buffer type for indication purpose
8973
let bytes_type = {
9074
// 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>) {
10084
"UNKNOWN"
10185
}
10286

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
10488
#[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+
},
10795
None => "RAW",
10896
}
10997
};

zenoh/src/api/query.rs

+6
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,12 @@ impl ReplyError {
8686
&self.payload
8787
}
8888

89+
/// Gets the mutable payload of this ReplyError.
90+
#[inline]
91+
pub fn payload_mut(&mut self) -> &mut ZBytes {
92+
&mut self.payload
93+
}
94+
8995
/// Gets the encoding of this ReplyError.
9096
#[inline]
9197
pub fn encoding(&self) -> &Encoding {

0 commit comments

Comments
 (0)