From 55c5e889e41ce087cd55cfb63d69c2a65e4d63b4 Mon Sep 17 00:00:00 2001 From: yellowhatter Date: Mon, 23 Jun 2025 10:53:06 +0300 Subject: [PATCH] Polish z_pub_shm.rs --- examples/examples/z_pub_shm.rs | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/examples/examples/z_pub_shm.rs b/examples/examples/z_pub_shm.rs index 30953843f..1f1f9a280 100644 --- a/examples/examples/z_pub_shm.rs +++ b/examples/examples/z_pub_shm.rs @@ -36,29 +36,26 @@ async fn main() -> zenoh::Result<()> { .wait() .unwrap(); + println!("Declaring Publisher on '{path}'..."); let publisher = session.declare_publisher(&path).await.unwrap(); - // Create allocation layout for series of similar allocations - println!("Allocating Shared Memory Buffer..."); - let layout = provider.alloc(1024).into_layout().unwrap(); - println!("Press CTRL-C to quit..."); for idx in 0..u32::MAX { tokio::time::sleep(std::time::Duration::from_secs(1)).await; - // Allocate particular SHM buffer using pre-created layout - let mut sbuf = layout - .alloc() - .with_policy::>() - .await - .unwrap(); - // We reserve a small space at the beginning of the buffer to include the iteration index // of the write. This is simply to have the same format as zn_pub. let prefix = format!("[{idx:4}] "); let prefix_len = prefix.len(); let slice_len = prefix_len + payload.len(); + // Allocate SHM buffer + let mut sbuf = provider + .alloc(slice_len) + .with_policy::>() + .await + .unwrap(); + sbuf[0..prefix_len].copy_from_slice(prefix.as_bytes()); sbuf[prefix_len..slice_len].copy_from_slice(payload.as_bytes());