Skip to content

Commit

Permalink
move Publisher.write method into trait (#736)
Browse files Browse the repository at this point in the history
* move Publisher.write method into HasWriteWithSampleKind trait

* fix broken test

* fix format
  • Loading branch information
DenisBiryukov91 authored Mar 13, 2024
1 parent 1c8ed89 commit ba50eec
Showing 1 changed file with 38 additions and 19 deletions.
57 changes: 38 additions & 19 deletions zenoh/src/publication.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,25 +318,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<IntoValue>(&self, kind: SampleKind, value: IntoValue) -> Publication
where
IntoValue: Into<Value>,
{
self._write(kind, value.into())
}

/// Put data.
///
/// # Examples
Expand Down Expand Up @@ -448,6 +429,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<IntoValue: Into<Value>>(
&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<IntoValue>(&self, kind: SampleKind, value: IntoValue) -> Self::WriteOutput<'_>
where
IntoValue: Into<Value>,
{
self._write(kind, value.into())
}
}

/// Functions to create zenoh entities with `'static` lifetime.
///
/// This trait contains functions to create zenoh entities like
Expand Down Expand Up @@ -1351,6 +1369,7 @@ mod tests {

#[test]
fn sample_kind_integrity_in_publication() {
use crate::publication::HasWriteWithSampleKind;
use crate::{open, prelude::sync::*};
use zenoh_protocol::core::SampleKind;

Expand Down

0 comments on commit ba50eec

Please sign in to comment.