Skip to content

Commit

Permalink
remove accessors in builders, adapt examples
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierre Avital committed Dec 13, 2023
1 parent 62633bf commit dd35540
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 84 deletions.
22 changes: 18 additions & 4 deletions examples/examples/z_pub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ async fn main() {
// Initiate logging
env_logger::init();

let (config, key_expr, value) = parse_args();
let (config, key_expr, value, attachment) = parse_args();

println!("Opening session...");
let session = zenoh::open(config).res().await.unwrap();
Expand All @@ -35,7 +35,16 @@ async fn main() {
sleep(Duration::from_secs(1)).await;
let buf = format!("[{idx:4}] {value}");
println!("Putting Data ('{}': '{}')...", &key_expr, buf);
publisher.put(buf).res().await.unwrap();
let mut put = publisher.put(buf);
if let Some(attachment) = &attachment {
put = put.with_attachment(
attachment
.split('&')
.map(|pair| pair.as_bytes().split_at(pair.find('=').unwrap_or(0)))
.collect(),
)
}
put.res().await.unwrap();
}
}

Expand All @@ -47,11 +56,16 @@ struct Args {
#[arg(short, long, default_value = "Pub from Rust!")]
/// The value to write.
value: String,
#[arg(short, long)]
/// The attachments to add to each put.
///
/// The key-value pairs are &-separated, and = serves as the separator between key and value.
attach: Option<String>,
#[command(flatten)]
common: CommonArgs,
}

fn parse_args() -> (Config, KeyExpr<'static>, String) {
fn parse_args() -> (Config, KeyExpr<'static>, String, Option<String>) {
let args = Args::parse();
(args.common.into(), args.key, args.value)
(args.common.into(), args.key, args.value, args.attach)
}
36 changes: 2 additions & 34 deletions examples/examples/z_pub_thr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ use clap::Parser;
use std::convert::TryInto;
use zenoh::prelude::sync::*;
use zenoh::publication::CongestionControl;
use zenoh::sample::AttachmentBuilder;
use zenoh_examples::CommonArgs;

fn main() {
Expand All @@ -29,12 +28,7 @@ fn main() {
prio = p.try_into().unwrap();
}

let mut payload_size = args.payload_size;
if args.attachments_number != 0 {
let mut att_size = 2 * args.attachments_number;
att_size += 2 + (core::mem::size_of::<usize>() * 8 - att_size.leading_zeros() as usize) / 7;
payload_size -= dbg!(att_size);
}
let payload_size = args.payload_size;

let data: Value = (0usize..dbg!(payload_size))
.map(|i| (i % 10) as u8)
Expand All @@ -53,25 +47,7 @@ fn main() {
let mut count: usize = 0;
let mut start = std::time::Instant::now();
loop {
let attachments = (args.attachments_number != 0).then(|| {
if args.attach_with_insert {
let mut attachments = AttachmentBuilder::new();
for _ in 0..args.attachments_number {
attachments.insert(b"", b"");
}
attachments.into()
} else {
std::iter::repeat((b"".as_slice(), b"".as_slice()))
.take(args.attachments_number)
.collect()
}
});

let mut put = publisher.put(data.clone());
if let Some(att) = attachments {
put = put.with_attachment(att)
}
put.res().unwrap();
publisher.put(data.clone()).res().unwrap();

if args.print {
if count < args.number {
Expand All @@ -97,14 +73,6 @@ struct Args {
/// Number of messages in each throughput measurements
#[arg(short, long, default_value = "100000")]
number: usize,
/// The number of attachments in the message.
///
/// The attachments will be sized such that the attachments replace the payload.
#[arg(long, default_value = "0")]
attachments_number: usize,
/// Attach through insert rather than FromIterator
#[arg(long)]
attach_with_insert: bool,
/// Sets the size of the payload to publish
payload_size: usize,
#[command(flatten)]
Expand Down
20 changes: 0 additions & 20 deletions zenoh/src/publication.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,16 +122,6 @@ impl PutBuilder<'_, '_> {
self
}

#[zenoh_macros::unstable]
pub fn attachment(&self) -> Option<&Attachment> {
self.attachment.as_ref()
}

#[zenoh_macros::unstable]
pub fn attachment_mut(&mut self) -> &mut Option<Attachment> {
&mut self.attachment
}

#[zenoh_macros::unstable]
pub fn with_attachment(mut self, attachment: Attachment) -> Self {
self.attachment = Some(attachment);
Expand Down Expand Up @@ -674,16 +664,6 @@ pub struct Publication<'a> {
}

impl<'a> Publication<'a> {
#[zenoh_macros::unstable]
pub fn attachment(&self) -> Option<&Attachment> {
self.attachment.as_ref()
}

#[zenoh_macros::unstable]
pub fn attachment_mut(&mut self) -> &mut Option<Attachment> {
&mut self.attachment
}

#[zenoh_macros::unstable]
pub fn with_attachment(mut self, attachment: Attachment) -> Self {
self.attachment = Some(attachment);
Expand Down
10 changes: 0 additions & 10 deletions zenoh/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,16 +306,6 @@ impl<'a, 'b, Handler> GetBuilder<'a, 'b, Handler> {
self
}

#[zenoh_macros::unstable]
pub fn attachment(&self) -> Option<&Attachment> {
self.attachment.as_ref()
}

#[zenoh_macros::unstable]
pub fn attachment_mut(&mut self) -> &mut Option<Attachment> {
&mut self.attachment
}

#[zenoh_macros::unstable]
pub fn with_attachment(mut self, attachment: Attachment) -> Self {
self.attachment = Some(attachment);
Expand Down
16 changes: 0 additions & 16 deletions zenoh/src/queryable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,22 +161,6 @@ pub struct ReplyBuilder<'a> {
}

impl<'a> ReplyBuilder<'a> {
#[zenoh_macros::unstable]
pub fn attachment(&self) -> Option<&Attachment> {
match &self.result {
Ok(sample) => sample.attachment.as_ref(),
Err(_) => None,
}
}

#[zenoh_macros::unstable]
pub fn attachment_mut(&mut self) -> Option<&mut Option<Attachment>> {
match &mut self.result {
Ok(sample) => Some(&mut sample.attachment),
Err(_) => None,
}
}

#[allow(clippy::result_large_err)]
#[zenoh_macros::unstable]
pub fn with_attachment(mut self, attachment: Attachment) -> Result<Self, (Self, Attachment)> {
Expand Down

0 comments on commit dd35540

Please sign in to comment.