Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move Publisher.write method into trait #736

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 38 additions & 19 deletions zenoh/src/publication.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<IntoValue>(&self, kind: SampleKind, value: IntoValue) -> Publication
where
IntoValue: Into<Value>,
{
self._write(kind, value.into())
}

/// Put data.
///
/// # Examples
Expand Down Expand Up @@ -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<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 @@ -1327,6 +1345,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