diff --git a/examples/examples/z_pub.rs b/examples/examples/z_pub.rs index d22d4d55ee..416ff31f46 100644 --- a/examples/examples/z_pub.rs +++ b/examples/examples/z_pub.rs @@ -39,7 +39,7 @@ async fn main() { println!("Putting Data ('{}': '{}')...", &key_expr, buf); let mut put = publisher.put(buf); if let Some(attachment) = &attachment { - put = put.with_attachment( + put = put.attachment( attachment .split('&') .map(|pair| split_once(pair, '=')) diff --git a/plugins/zenoh-plugin-storage-manager/src/replica/align_queryable.rs b/plugins/zenoh-plugin-storage-manager/src/replica/align_queryable.rs index e5c4840666..973fb89abe 100644 --- a/plugins/zenoh-plugin-storage-manager/src/replica/align_queryable.rs +++ b/plugins/zenoh-plugin-storage-manager/src/replica/align_queryable.rs @@ -130,7 +130,7 @@ impl AlignQueryable { query .reply(k, v.payload) .with_encoding(v.encoding) - .with_timestamp(ts) + .timestamp(ts) .res() .await .unwrap(); diff --git a/plugins/zenoh-plugin-storage-manager/src/replica/aligner.rs b/plugins/zenoh-plugin-storage-manager/src/replica/aligner.rs index 4119a941e5..9d5257e53f 100644 --- a/plugins/zenoh-plugin-storage-manager/src/replica/aligner.rs +++ b/plugins/zenoh-plugin-storage-manager/src/replica/aligner.rs @@ -112,7 +112,7 @@ impl Aligner { } = value; let sample = PutSampleBuilder::new(key, payload) .with_encoding(encoding) - .with_timestamp(ts) + .timestamp(ts) .res_sync(); log::debug!("[ALIGNER] Adding {:?} to storage", sample); self.tx_sample.send_async(sample).await.unwrap_or_else(|e| { diff --git a/plugins/zenoh-plugin-storage-manager/src/replica/storage.rs b/plugins/zenoh-plugin-storage-manager/src/replica/storage.rs index 69c973de39..de76ade51d 100644 --- a/plugins/zenoh-plugin-storage-manager/src/replica/storage.rs +++ b/plugins/zenoh-plugin-storage-manager/src/replica/storage.rs @@ -239,7 +239,7 @@ impl StorageService { } }; let timestamp = sample.timestamp().cloned().unwrap_or(new_reception_timestamp()); - let sample = SampleBuilder::from(sample).with_timestamp(timestamp).res_sync(); + let sample = SampleBuilder::from(sample).timestamp(timestamp).res_sync(); self.process_sample(sample).await; }, // on query on key_expr @@ -316,14 +316,14 @@ impl StorageService { } = data.value; PutSampleBuilder::new(KeyExpr::from(k.clone()), payload) .with_encoding(encoding) - .with_timestamp(data.timestamp) + .timestamp(data.timestamp) .res_sync() } Some(Update { kind: SampleKind::Delete, data, }) => DeleteSampleBuilder::new(KeyExpr::from(k.clone())) - .with_timestamp(data.timestamp) + .timestamp(data.timestamp) .res_sync(), None => SampleBuilder::from(sample.clone()) .keyexpr(k.clone()) @@ -533,7 +533,7 @@ impl StorageService { if let Err(e) = q .reply(key.clone(), payload) .with_encoding(encoding) - .with_timestamp(entry.timestamp) + .timestamp(entry.timestamp) .res_async() .await { @@ -568,7 +568,7 @@ impl StorageService { if let Err(e) = q .reply(q.key_expr().clone(), payload) .with_encoding(encoding) - .with_timestamp(entry.timestamp) + .timestamp(entry.timestamp) .res_async() .await { diff --git a/zenoh-ext/src/querying_subscriber.rs b/zenoh-ext/src/querying_subscriber.rs index e6b269cfbd..52a4263396 100644 --- a/zenoh-ext/src/querying_subscriber.rs +++ b/zenoh-ext/src/querying_subscriber.rs @@ -665,7 +665,7 @@ impl<'a, Receiver> FetchingSubscriber<'a, Receiver> { // ensure the sample has a timestamp, thus it will always be sorted into the MergeQueue // after any timestamped Sample possibly coming from a fetch reply. let timestamp = s.timestamp().cloned().unwrap_or(new_reception_timestamp()); - let s = SampleBuilder::from(s).with_timestamp(timestamp).res_sync(); + let s = SampleBuilder::from(s).timestamp(timestamp).res_sync(); state.merge_queue.push(s); } } diff --git a/zenoh/src/publication.rs b/zenoh/src/publication.rs index f8a42077b9..cd68530bf7 100644 --- a/zenoh/src/publication.rs +++ b/zenoh/src/publication.rs @@ -147,54 +147,52 @@ impl QoSBuilderTrait for DeleteBuilder<'_, '_> { } impl TimestampBuilderTrait for PutBuilder<'_, '_> { - fn with_timestamp(self, timestamp: Option) -> Self { - Self { timestamp, ..self } + fn timestamp>>(self, timestamp: T) -> Self { + Self { + timestamp: timestamp.into(), + ..self + } } } impl SampleBuilderTrait for PutBuilder<'_, '_> { #[cfg(feature = "unstable")] - fn with_source_info(self, source_info: SourceInfo) -> Self { + fn source_info(self, source_info: SourceInfo) -> Self { Self { source_info, ..self } } #[cfg(feature = "unstable")] - fn with_attachment_opt(self, attachment: Option) -> Self { - Self { attachment, ..self } - } - #[cfg(feature = "unstable")] - fn with_attachment(self, attachment: Attachment) -> Self { + fn attachment>>(self, attachment: T) -> Self { Self { - attachment: Some(attachment), + attachment: attachment.into(), ..self } } } impl TimestampBuilderTrait for DeleteBuilder<'_, '_> { - fn with_timestamp(self, timestamp: Option) -> Self { - Self { timestamp, ..self } + fn timestamp>>(self, timestamp: T) -> Self { + Self { + timestamp: timestamp.into(), + ..self + } } } impl SampleBuilderTrait for DeleteBuilder<'_, '_> { #[cfg(feature = "unstable")] - fn with_source_info(self, source_info: SourceInfo) -> Self { + fn source_info(self, source_info: SourceInfo) -> Self { Self { source_info, ..self } } #[cfg(feature = "unstable")] - fn with_attachment_opt(self, attachment: Option) -> Self { - Self { attachment, ..self } - } - #[cfg(feature = "unstable")] - fn with_attachment(self, attachment: Attachment) -> Self { + fn attachment>>(self, attachment: T) -> Self { Self { - attachment: Some(attachment), + attachment: attachment.into(), ..self } } @@ -754,14 +752,17 @@ pub struct DeletePublication<'a> { } impl TimestampBuilderTrait for PutPublication<'_> { - fn with_timestamp(self, timestamp: Option) -> Self { - Self { timestamp, ..self } + fn timestamp>>(self, timestamp: T) -> Self { + Self { + timestamp: timestamp.into(), + ..self + } } } impl SampleBuilderTrait for PutPublication<'_> { #[cfg(feature = "unstable")] - fn with_source_info(self, source_info: SourceInfo) -> Self { + fn source_info(self, source_info: SourceInfo) -> Self { Self { source_info, ..self @@ -769,14 +770,9 @@ impl SampleBuilderTrait for PutPublication<'_> { } #[cfg(feature = "unstable")] - fn with_attachment_opt(self, attachment: Option) -> Self { - Self { attachment, ..self } - } - - #[cfg(feature = "unstable")] - fn with_attachment(self, attachment: Attachment) -> Self { + fn attachment>>(self, attachment: T) -> Self { Self { - attachment: Some(attachment), + attachment: attachment.into(), ..self } } @@ -799,14 +795,17 @@ impl ValueBuilderTrait for PutPublication<'_> { } impl TimestampBuilderTrait for DeletePublication<'_> { - fn with_timestamp(self, timestamp: Option) -> Self { - Self { timestamp, ..self } + fn timestamp>>(self, timestamp: T) -> Self { + Self { + timestamp: timestamp.into(), + ..self + } } } impl SampleBuilderTrait for DeletePublication<'_> { #[cfg(feature = "unstable")] - fn with_source_info(self, source_info: SourceInfo) -> Self { + fn source_info(self, source_info: SourceInfo) -> Self { Self { source_info, ..self @@ -814,14 +813,9 @@ impl SampleBuilderTrait for DeletePublication<'_> { } #[cfg(feature = "unstable")] - fn with_attachment_opt(self, attachment: Option) -> Self { - Self { attachment, ..self } - } - - #[cfg(feature = "unstable")] - fn with_attachment(self, attachment: Attachment) -> Self { + fn attachment>>(self, attachment: T) -> Self { Self { - attachment: Some(attachment), + attachment: attachment.into(), ..self } } diff --git a/zenoh/src/query.rs b/zenoh/src/query.rs index db17715a89..2d4e5e1ee3 100644 --- a/zenoh/src/query.rs +++ b/zenoh/src/query.rs @@ -135,7 +135,7 @@ pub struct GetBuilder<'a, 'b, Handler> { impl SampleBuilderTrait for GetBuilder<'_, '_, Handler> { #[cfg(feature = "unstable")] - fn with_source_info(self, source_info: SourceInfo) -> Self { + fn source_info(self, source_info: SourceInfo) -> Self { Self { source_info, ..self @@ -143,14 +143,9 @@ impl SampleBuilderTrait for GetBuilder<'_, '_, Handler> { } #[cfg(feature = "unstable")] - fn with_attachment_opt(self, attachment: Option) -> Self { - Self { attachment, ..self } - } - - #[cfg(feature = "unstable")] - fn with_attachment(self, attachment: Attachment) -> Self { + fn attachment>>(self, attachment: T) -> Self { Self { - attachment: Some(attachment), + attachment: attachment.into(), ..self } } diff --git a/zenoh/src/queryable.rs b/zenoh/src/queryable.rs index 625ae6f25f..66cb34459b 100644 --- a/zenoh/src/queryable.rs +++ b/zenoh/src/queryable.rs @@ -239,9 +239,9 @@ impl<'a> ReplySampleBuilder<'a> { } impl TimestampBuilderTrait for ReplySampleBuilder<'_> { - fn with_timestamp(self, timestamp: Option) -> Self { + fn timestamp>>(self, timestamp: T) -> Self { Self { - sample_builder: self.sample_builder.with_timestamp(timestamp), + sample_builder: self.sample_builder.timestamp(timestamp), ..self } } @@ -249,25 +249,17 @@ impl TimestampBuilderTrait for ReplySampleBuilder<'_> { impl SampleBuilderTrait for ReplySampleBuilder<'_> { #[cfg(feature = "unstable")] - fn with_source_info(self, source_info: SourceInfo) -> Self { + fn source_info(self, source_info: SourceInfo) -> Self { Self { - sample_builder: self.sample_builder.with_source_info(source_info), + sample_builder: self.sample_builder.source_info(source_info), ..self } } #[cfg(feature = "unstable")] - fn with_attachment_opt(self, attachment: Option) -> Self { + fn attachment>>(self, attachment: T) -> Self { Self { - sample_builder: self.sample_builder.with_attachment_opt(attachment), - ..self - } - } - - #[cfg(feature = "unstable")] - fn with_attachment(self, attachment: Attachment) -> Self { - Self { - sample_builder: self.sample_builder.with_attachment(attachment), + sample_builder: self.sample_builder.attachment(attachment), ..self } } @@ -324,9 +316,9 @@ pub struct ReplyBuilder<'a> { } impl TimestampBuilderTrait for ReplyBuilder<'_> { - fn with_timestamp(self, timestamp: Option) -> Self { + fn timestamp>>(self, timestamp: T) -> Self { Self { - sample_builder: self.sample_builder.with_timestamp(timestamp), + sample_builder: self.sample_builder.timestamp(timestamp), ..self } } @@ -334,25 +326,17 @@ impl TimestampBuilderTrait for ReplyBuilder<'_> { impl SampleBuilderTrait for ReplyBuilder<'_> { #[cfg(feature = "unstable")] - fn with_source_info(self, source_info: SourceInfo) -> Self { - Self { - sample_builder: self.sample_builder.with_source_info(source_info), - ..self - } - } - - #[cfg(feature = "unstable")] - fn with_attachment_opt(self, attachment: Option) -> Self { + fn source_info(self, source_info: SourceInfo) -> Self { Self { - sample_builder: self.sample_builder.with_attachment_opt(attachment), + sample_builder: self.sample_builder.source_info(source_info), ..self } } #[cfg(feature = "unstable")] - fn with_attachment(self, attachment: Attachment) -> Self { + fn attachment>>(self, attachment: T) -> Self { Self { - sample_builder: self.sample_builder.with_attachment(attachment), + sample_builder: self.sample_builder.attachment(attachment), ..self } } @@ -409,9 +393,9 @@ pub struct ReplyDelBuilder<'a> { } impl TimestampBuilderTrait for ReplyDelBuilder<'_> { - fn with_timestamp(self, timestamp: Option) -> Self { + fn timestamp>>(self, timestamp: T) -> Self { Self { - sample_builder: self.sample_builder.with_timestamp(timestamp), + sample_builder: self.sample_builder.timestamp(timestamp), ..self } } @@ -419,25 +403,17 @@ impl TimestampBuilderTrait for ReplyDelBuilder<'_> { impl SampleBuilderTrait for ReplyDelBuilder<'_> { #[cfg(feature = "unstable")] - fn with_source_info(self, source_info: SourceInfo) -> Self { - Self { - sample_builder: self.sample_builder.with_source_info(source_info), - ..self - } - } - - #[cfg(feature = "unstable")] - fn with_attachment_opt(self, attachment: Option) -> Self { + fn source_info(self, source_info: SourceInfo) -> Self { Self { - sample_builder: self.sample_builder.with_attachment_opt(attachment), + sample_builder: self.sample_builder.source_info(source_info), ..self } } #[cfg(feature = "unstable")] - fn with_attachment(self, attachment: Attachment) -> Self { + fn attachment>>(self, attachment: T) -> Self { Self { - sample_builder: self.sample_builder.with_attachment(attachment), + sample_builder: self.sample_builder.attachment(attachment), ..self } } diff --git a/zenoh/src/sample.rs b/zenoh/src/sample.rs index d774e5e007..163ae2090a 100644 --- a/zenoh/src/sample.rs +++ b/zenoh/src/sample.rs @@ -263,6 +263,17 @@ mod attachment { } } } + #[zenoh_macros::unstable] + impl From for Option { + fn from(value: AttachmentBuilder) -> Self { + if value.inner.is_empty() { + None + } else { + Some(value.into()) + } + } + } + #[zenoh_macros::unstable] #[derive(Clone)] pub struct Attachment { diff --git a/zenoh/src/sample_builder.rs b/zenoh/src/sample_builder.rs index 990586ca0f..2d7277506d 100644 --- a/zenoh/src/sample_builder.rs +++ b/zenoh/src/sample_builder.rs @@ -41,20 +41,17 @@ pub trait QoSBuilderTrait { } pub trait TimestampBuilderTrait { - /// Sets of clears timestamp - fn with_timestamp(self, timestamp: Option) -> Self; + /// Sets of clears timestamp + fn timestamp>>(self, timestamp: T) -> Self; } pub trait SampleBuilderTrait { /// Attach source information #[zenoh_macros::unstable] - fn with_source_info(self, source_info: SourceInfo) -> Self; - /// Attach or remove user-provided data in key-value format - #[zenoh_macros::unstable] - fn with_attachment_opt(self, attachment: Option) -> Self; + fn source_info(self, source_info: SourceInfo) -> Self; /// Attach user-provided data in key-value format #[zenoh_macros::unstable] - fn with_attachment(self, attachment: Attachment) -> Self; + fn attachment>>(self, attachment: T) -> Self; } pub trait ValueBuilderTrait { @@ -100,9 +97,9 @@ impl SampleBuilder { } impl TimestampBuilderTrait for SampleBuilder { - fn with_timestamp(self, timestamp: Option) -> Self { + fn timestamp>>(self, timestamp: T) -> Self { Self(Sample { - timestamp, + timestamp: timestamp.into(), ..self.0 }) } @@ -110,7 +107,7 @@ impl TimestampBuilderTrait for SampleBuilder { impl SampleBuilderTrait for SampleBuilder { #[zenoh_macros::unstable] - fn with_source_info(self, source_info: SourceInfo) -> Self { + fn source_info(self, source_info: SourceInfo) -> Self { Self(Sample { source_info, ..self.0 @@ -118,17 +115,12 @@ impl SampleBuilderTrait for SampleBuilder { } #[zenoh_macros::unstable] - fn with_attachment_opt(self, attachment: Option) -> Self { + fn attachment>>(self, attachment: T) -> Self { Self(Sample { - attachment, + attachment: attachment.into(), ..self.0 }) } - - #[zenoh_macros::unstable] - fn with_attachment(self, attachment: Attachment) -> Self { - self.with_attachment_opt(Some(attachment)) - } } impl QoSBuilderTrait for SampleBuilder { @@ -194,23 +186,19 @@ impl PutSampleBuilder { } impl TimestampBuilderTrait for PutSampleBuilder { - fn with_timestamp(self, timestamp: Option) -> Self { - Self(self.0.with_timestamp(timestamp)) + fn timestamp>>(self, timestamp: T) -> Self { + Self(self.0.timestamp(timestamp)) } } impl SampleBuilderTrait for PutSampleBuilder { #[zenoh_macros::unstable] - fn with_source_info(self, source_info: SourceInfo) -> Self { - Self(self.0.with_source_info(source_info)) + fn source_info(self, source_info: SourceInfo) -> Self { + Self(self.0.source_info(source_info)) } #[zenoh_macros::unstable] - fn with_attachment(self, attachment: Attachment) -> Self { - Self(self.0.with_attachment(attachment)) - } - #[zenoh_macros::unstable] - fn with_attachment_opt(self, attachment: Option) -> Self { - Self(self.0.with_attachment_opt(attachment)) + fn attachment>>(self, attachment: T) -> Self { + Self(self.0.attachment(attachment)) } } @@ -288,23 +276,19 @@ impl DeleteSampleBuilder { } impl TimestampBuilderTrait for DeleteSampleBuilder { - fn with_timestamp(self, timestamp: Option) -> Self { - Self(self.0.with_timestamp(timestamp)) + fn timestamp>>(self, timestamp: T) -> Self { + Self(self.0.timestamp(timestamp)) } } impl SampleBuilderTrait for DeleteSampleBuilder { #[zenoh_macros::unstable] - fn with_source_info(self, source_info: SourceInfo) -> Self { - Self(self.0.with_source_info(source_info)) - } - #[zenoh_macros::unstable] - fn with_attachment(self, attachment: Attachment) -> Self { - Self(self.0.with_attachment(attachment)) + fn source_info(self, source_info: SourceInfo) -> Self { + Self(self.0.source_info(source_info)) } #[zenoh_macros::unstable] - fn with_attachment_opt(self, attachment: Option) -> Self { - Self(self.0.with_attachment_opt(attachment)) + fn attachment>>(self, attachment: T) -> Self { + Self(self.0.attachment(attachment)) } } diff --git a/zenoh/tests/attachments.rs b/zenoh/tests/attachments.rs index ba4c8a7d7c..e87fc5243b 100644 --- a/zenoh/tests/attachments.rs +++ b/zenoh/tests/attachments.rs @@ -38,22 +38,22 @@ fn pubsub() { } zenoh .put("test/attachment", "put") - .with_attachment( + .attachment(Some( backer .iter() .map(|b| (b.0.as_slice(), b.1.as_slice())) .collect(), - ) + )) .res() .unwrap(); publisher .put("publisher") - .with_attachment( + .attachment(Some( backer .iter() .map(|b| (b.0.as_slice(), b.1.as_slice())) .collect(), - ) + )) .res() .unwrap(); } @@ -84,7 +84,7 @@ fn queries() { query.key_expr().clone(), query.value().unwrap().payload.clone(), ) - .with_attachment(attachment) + .attachment(attachment) .res() .unwrap(); })