diff --git a/examples/examples/z_get.rs b/examples/examples/z_get.rs index dce74d367b..074f931eff 100644 --- a/examples/examples/z_get.rs +++ b/examples/examples/z_get.rs @@ -28,15 +28,18 @@ async fn main() { let session = zenoh::open(config).res().await.unwrap(); println!("Sending Query '{selector}'..."); - let replies = match value { - Some(value) => session.get(&selector).with_value(value), - None => session.get(&selector), - } - .target(target) - .timeout(timeout) - .res() - .await - .unwrap(); + // let replies = match value { + // Some(value) => session.get(&selector).payload(value), + // None => session.get(&selector), + // } + let replies = session + .get(&selector) + .value(value.map(Value::from)) + .target(target) + .timeout(timeout) + .res() + .await + .unwrap(); while let Ok(reply) = replies.recv_async().await { match reply.sample { Ok(sample) => { diff --git a/plugins/zenoh-plugin-rest/src/lib.rs b/plugins/zenoh-plugin-rest/src/lib.rs index 94796c518d..f78c541eff 100644 --- a/plugins/zenoh-plugin-rest/src/lib.rs +++ b/plugins/zenoh-plugin-rest/src/lib.rs @@ -420,7 +420,7 @@ async fn query(mut req: Request<(Arc, String)>) -> tide::Result { diff --git a/zenoh/src/publication.rs b/zenoh/src/publication.rs index d2463610fb..103a65e782 100644 --- a/zenoh/src/publication.rs +++ b/zenoh/src/publication.rs @@ -215,6 +215,14 @@ impl ValueBuilderTrait for PutBuilder<'_, '_> { ..self } } + fn value>(self, value: T) -> Self { + let Value { payload, encoding } = value.into(); + Self { + payload, + encoding, + ..self + } + } } impl PutBuilder<'_, '_> { @@ -798,6 +806,15 @@ impl ValueBuilderTrait for PutPublication<'_> { ..self } } + + fn value>(self, value: T) -> Self { + let Value { payload, encoding } = value.into(); + Self { + payload, + encoding, + ..self + } + } } impl TimestampBuilderTrait for DeletePublication<'_> { diff --git a/zenoh/src/query.rs b/zenoh/src/query.rs index 3a7ee771b3..5a1d443463 100644 --- a/zenoh/src/query.rs +++ b/zenoh/src/query.rs @@ -178,6 +178,13 @@ impl ValueBuilderTrait for GetBuilder<'_, '_, Handler> { let value = Some(self.value.unwrap_or_default().payload(payload)); Self { value, ..self } } + fn value>(self, value: T) -> Self { + let value: Value = value.into(); + Self { + value: if value.is_empty() { None } else { Some(value) }, + ..self + } + } } impl<'a, 'b> GetBuilder<'a, 'b, DefaultHandler> { @@ -328,48 +335,34 @@ impl<'a, 'b> GetBuilder<'a, 'b, DefaultHandler> { impl<'a, 'b, Handler> GetBuilder<'a, 'b, Handler> { /// Change the target of the query. #[inline] - pub fn target(mut self, target: QueryTarget) -> Self { - self.target = target; - self + pub fn target(self, target: QueryTarget) -> Self { + Self { target, ..self } } /// Change the consolidation mode of the query. #[inline] - pub fn consolidation>(mut self, consolidation: QC) -> Self { - self.consolidation = consolidation.into(); - self + pub fn consolidation>(self, consolidation: QC) -> Self { + Self { + consolidation: consolidation.into(), + ..self + } } /// Restrict the matching queryables that will receive the query /// to the ones that have the given [`Locality`](crate::prelude::Locality). #[zenoh_macros::unstable] #[inline] - pub fn allowed_destination(mut self, destination: Locality) -> Self { - self.destination = destination; - self + pub fn allowed_destination(self, destination: Locality) -> Self { + Self { + destination, + ..self + } } /// Set query timeout. #[inline] - pub fn timeout(mut self, timeout: Duration) -> Self { - self.timeout = timeout; - self - } - - /// Set query value. - #[inline] - pub fn with_value(mut self, value: IntoValue) -> Self - where - IntoValue: Into, - { - self.value = Some(value.into()); - self - } - - #[zenoh_macros::unstable] - pub fn with_attachment(mut self, attachment: Attachment) -> Self { - self.attachment = Some(attachment); - self + pub fn timeout(self, timeout: Duration) -> Self { + Self { timeout, ..self } } /// By default, `get` guarantees that it will only receive replies whose key expressions intersect diff --git a/zenoh/src/queryable.rs b/zenoh/src/queryable.rs index c9492394c4..aa5f041a2b 100644 --- a/zenoh/src/queryable.rs +++ b/zenoh/src/queryable.rs @@ -378,6 +378,13 @@ impl ValueBuilderTrait for ReplyBuilder<'_> { ..self } } + fn value>(self, value: T) -> Self { + let Value { payload, encoding } = value.into(); + Self { + sample_builder: self.sample_builder.payload(payload).encoding(encoding), + ..self + } + } } /// A builder returned by [`Query::reply_del()`](Query::reply) diff --git a/zenoh/src/sample/builder.rs b/zenoh/src/sample/builder.rs index 8c507c8119..1bd50e7f69 100644 --- a/zenoh/src/sample/builder.rs +++ b/zenoh/src/sample/builder.rs @@ -22,6 +22,7 @@ use crate::Payload; use crate::Priority; use crate::Sample; use crate::SampleKind; +use crate::Value; use uhlc::Timestamp; use zenoh_core::zresult; use zenoh_protocol::core::CongestionControl; @@ -56,6 +57,9 @@ pub trait ValueBuilderTrait { fn encoding>(self, encoding: T) -> Self; /// Sets the payload fn payload>(self, payload: T) -> Self; + /// Sets both payload and encoding at once. + /// This is convenient for passing user type which supports `Into` when both payload and encoding depends on user type + fn value>(self, value: T) -> Self; } #[derive(Debug)] @@ -221,6 +225,14 @@ impl ValueBuilderTrait for PutSampleBuilder { ..self.0 .0 })) } + fn value>(self, value: T) -> Self { + let Value { payload, encoding } = value.into(); + Self(SampleBuilder(Sample { + payload, + encoding, + ..self.0 .0 + })) + } } #[derive(Debug)] diff --git a/zenoh/src/value.rs b/zenoh/src/value.rs index 8ea5aef19f..92a87cb6c5 100644 --- a/zenoh/src/value.rs +++ b/zenoh/src/value.rs @@ -43,6 +43,11 @@ impl Value { encoding: Encoding::default(), } } + /// Checks if the [`Value`] is empty. + /// Value is considered empty if its payload is empty and encoding is default. + pub fn is_empty(&self) -> bool { + self.payload.is_empty() && self.encoding == Encoding::default() + } } impl ValueBuilderTrait for Value { @@ -58,6 +63,10 @@ impl ValueBuilderTrait for Value { ..self } } + fn value>(self, value: T) -> Self { + let Value { payload, encoding } = value.into(); + Self { payload, encoding } + } } impl From for Value @@ -72,6 +81,15 @@ where } } +impl From> for Value +where + T: Into, +{ + fn from(t: Option) -> Self { + t.map_or_else(Value::empty, Into::into) + } +} + impl Default for Value { fn default() -> Self { Value::empty() diff --git a/zenoh/tests/attachments.rs b/zenoh/tests/attachments.rs index f50e33cf6f..2725351ab0 100644 --- a/zenoh/tests/attachments.rs +++ b/zenoh/tests/attachments.rs @@ -100,13 +100,13 @@ fn queries() { } let get = zenoh .get("test/attachment") - .with_value("query") - .with_attachment( + .payload("query") + .attachment(Some( backer .iter() .map(|b| (b.0.as_slice(), b.1.as_slice())) .collect(), - ) + )) .res() .unwrap(); while let Ok(reply) = get.recv() { diff --git a/zenoh/tests/handler.rs b/zenoh/tests/handler.rs index c1e912fc75..ceed15e2c3 100644 --- a/zenoh/tests/handler.rs +++ b/zenoh/tests/handler.rs @@ -57,12 +57,12 @@ fn query_with_ringbuffer() { let _reply1 = zenoh .get("test/ringbuffer_query") - .with_value("query1") + .payload("query1") .res() .unwrap(); let _reply2 = zenoh .get("test/ringbuffer_query") - .with_value("query2") + .payload("query2") .res() .unwrap();