Skip to content

Polish z_pub_shm.rs #2007

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 8 additions & 11 deletions examples/examples/z_pub_shm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<BlockOn<GarbageCollect>>()
.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::<BlockOn<GarbageCollect>>()
.await
.unwrap();

sbuf[0..prefix_len].copy_from_slice(prefix.as_bytes());
sbuf[prefix_len..slice_len].copy_from_slice(payload.as_bytes());

Expand Down
Loading