From e547aad6f5b746725011a1ccbf5a53ae5be47952 Mon Sep 17 00:00:00 2001 From: Denis Biryukov Date: Mon, 12 Feb 2024 11:59:36 +0100 Subject: [PATCH] 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