From e547aad6f5b746725011a1ccbf5a53ae5be47952 Mon Sep 17 00:00:00 2001 From: Denis Biryukov Date: Mon, 12 Feb 2024 11:59:36 +0100 Subject: [PATCH 1/3] move Publisher.write method into HasWriteWithSampleKind trait --- zenoh/src/publication.rs | 56 ++++++++++++++++++++++++++-------------- 1 file changed, 37 insertions(+), 19 deletions(-) diff --git a/zenoh/src/publication.rs b/zenoh/src/publication.rs index 843190ad45..c350499ec8 100644 --- a/zenoh/src/publication.rs +++ b/zenoh/src/publication.rs @@ -317,25 +317,6 @@ impl<'a> Publisher<'a> { } } - /// Send data with [`kind`](SampleKind) (Put or Delete). - /// - /// # Examples - /// ``` - /// # async_std::task::block_on(async { - /// use zenoh::prelude::r#async::*; - /// - /// let session = zenoh::open(config::peer()).res().await.unwrap().into_arc(); - /// let publisher = session.declare_publisher("key/expression").res().await.unwrap(); - /// publisher.write(SampleKind::Put, "value").res().await.unwrap(); - /// # }) - /// ``` - pub fn write(&self, kind: SampleKind, value: IntoValue) -> Publication - where - IntoValue: Into, - { - self._write(kind, value.into()) - } - /// Put data. /// /// # Examples @@ -447,6 +428,43 @@ impl<'a> Publisher<'a> { } } +/// Internal function for sending data with specified [`kind`](SampleKind) +pub trait HasWriteWithSampleKind { + type WriteOutput<'a> + where + Self: 'a; + fn write>( + &self, + kind: SampleKind, + value: IntoValue, + ) -> Self::WriteOutput<'_>; +} + +impl<'a> HasWriteWithSampleKind for Publisher<'a> { + type WriteOutput<'b> = Publication<'b> + where + 'a: 'b; + /// Send data with [`kind`](SampleKind) (Put or Delete). + /// + /// # Examples + /// ``` + /// # async_std::task::block_on(async { + /// use zenoh::prelude::r#async::*; + /// use zenoh::publication::HasWriteWithSampleKind; + /// + /// let session = zenoh::open(config::peer()).res().await.unwrap().into_arc(); + /// let publisher = session.declare_publisher("key/expression").res().await.unwrap(); + /// publisher.write(SampleKind::Put, "value").res().await.unwrap(); + /// # }) + /// ``` + fn write(&self, kind: SampleKind, value: IntoValue) -> Self::WriteOutput<'_> + where + IntoValue: Into, + { + self._write(kind, value.into()) + } +} + /// Functions to create zenoh entities with `'static` lifetime. /// /// This trait contains functions to create zenoh entities like From 4b14ea39645d3d35796265b87a99363e15544a6c Mon Sep 17 00:00:00 2001 From: Denis Biryukov Date: Tue, 13 Feb 2024 16:20:30 +0100 Subject: [PATCH 2/3] fix broken test --- zenoh/src/publication.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/zenoh/src/publication.rs b/zenoh/src/publication.rs index c350499ec8..b4d19e5b7b 100644 --- a/zenoh/src/publication.rs +++ b/zenoh/src/publication.rs @@ -1347,6 +1347,7 @@ mod tests { fn sample_kind_integrity_in_publication() { use crate::{open, prelude::sync::*}; use zenoh_protocol::core::SampleKind; + use crate::publication::HasWriteWithSampleKind; const KEY_EXPR: &str = "test/sample_kind_integrity/publication"; const VALUE: &str = "zenoh"; From b5ad726df7cca6e84b9c2c633802509a72f1d79b Mon Sep 17 00:00:00 2001 From: Denis Biryukov Date: Tue, 13 Feb 2024 18:43:46 +0100 Subject: [PATCH 3/3] fix format --- zenoh/src/publication.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/zenoh/src/publication.rs b/zenoh/src/publication.rs index b4d19e5b7b..5a8264d592 100644 --- a/zenoh/src/publication.rs +++ b/zenoh/src/publication.rs @@ -1345,9 +1345,9 @@ mod tests { #[test] fn sample_kind_integrity_in_publication() { + use crate::publication::HasWriteWithSampleKind; use crate::{open, prelude::sync::*}; use zenoh_protocol::core::SampleKind; - use crate::publication::HasWriteWithSampleKind; const KEY_EXPR: &str = "test/sample_kind_integrity/publication"; const VALUE: &str = "zenoh";