Skip to content

Commit

Permalink
feat: add auto_encoding method to builder ot set encoding based fro…
Browse files Browse the repository at this point in the history
…m payload type
  • Loading branch information
wyfo committed Jun 10, 2024
1 parent ad6c414 commit 08c207e
Show file tree
Hide file tree
Showing 12 changed files with 394 additions and 289 deletions.
1 change: 1 addition & 0 deletions zenoh/src/api/builders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@
// ZettaScale Zenoh Team, <[email protected]>
//

pub(crate) mod encoding;
pub(crate) mod publisher;
pub(crate) mod sample;
22 changes: 22 additions & 0 deletions zenoh/src/api/builders/encoding.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use crate::api::encoding::{AutoEncoding, Encoding};

pub trait EncodingBuilderTrait {
/// Set the [`Encoding`]
fn encoding<T: Into<Encoding>>(self, encoding: T) -> Self;
}

pub trait AutoEncodingBuilderTrait<Payload>: EncodingBuilderTrait + Sized
where
Payload: AutoEncoding,
{
/// Set the [`Encoding`] according to the payload type
fn auto_encoding(self) -> Self {
if let Some(payload) = self.get_payload() {
let encoding = payload.get_encoding();
return self.encoding(encoding);
}
self
}
#[doc(hidden)]
fn get_payload(&self) -> Option<&Payload>;
}
86 changes: 45 additions & 41 deletions zenoh/src/api/builders/publisher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,33 @@ use zenoh_protocol::{core::CongestionControl, network::Mapping};
#[cfg(feature = "unstable")]
use crate::api::sample::SourceInfo;
use crate::api::{
builders::sample::{
QoSBuilderTrait, SampleBuilderTrait, TimestampBuilderTrait, ValueBuilderTrait,
builders::{
encoding::{AutoEncodingBuilderTrait, EncodingBuilderTrait},
sample::{QoSBuilderTrait, SampleBuilderTrait, TimestampBuilderTrait},
},
bytes::{OptionZBytes, ZBytes},
encoding::Encoding,
encoding::{AutoEncoding, Encoding},
key_expr::KeyExpr,
publisher::{Priority, Publisher},
sample::{Locality, SampleKind},
session::SessionRef,
value::Value,
};

pub type SessionPutBuilder<'a, 'b> =
PublicationBuilder<PublisherBuilder<'a, 'b>, PublicationBuilderPut>;
pub type SessionPutBuilder<'a, 'b, Payload> =
PublicationBuilder<PublisherBuilder<'a, 'b>, PublicationBuilderPut<Payload>>;

pub type SessionDeleteBuilder<'a, 'b> =
PublicationBuilder<PublisherBuilder<'a, 'b>, PublicationBuilderDelete>;

pub type PublisherPutBuilder<'a> = PublicationBuilder<&'a Publisher<'a>, PublicationBuilderPut>;
pub type PublisherPutBuilder<'a, Payload> =
PublicationBuilder<&'a Publisher<'a>, PublicationBuilderPut<Payload>>;

pub type PublisherDeleteBuilder<'a> =
PublicationBuilder<&'a Publisher<'a>, PublicationBuilderDelete>;

#[derive(Debug, Clone)]
pub struct PublicationBuilderPut {
pub(crate) payload: ZBytes,
pub struct PublicationBuilderPut<Payload> {
pub(crate) payload: Payload,
pub(crate) encoding: Encoding,
}
#[derive(Debug, Clone)]
Expand All @@ -70,16 +71,16 @@ pub struct PublicationBuilderDelete;
/// ```
#[must_use = "Resolvables do nothing unless you resolve them using the `res` method from either `SyncResolve` or `AsyncResolve`"]
#[derive(Debug, Clone)]
pub struct PublicationBuilder<P, T> {
pub struct PublicationBuilder<P, Kind> {
pub(crate) publisher: P,
pub(crate) kind: T,
pub(crate) kind: Kind,
pub(crate) timestamp: Option<uhlc::Timestamp>,
#[cfg(feature = "unstable")]
pub(crate) source_info: SourceInfo,
pub(crate) attachment: Option<ZBytes>,
}

impl<T> QoSBuilderTrait for PublicationBuilder<PublisherBuilder<'_, '_>, T> {
impl<Kind> QoSBuilderTrait for PublicationBuilder<PublisherBuilder<'_, '_>, Kind> {
#[inline]
fn congestion_control(self, congestion_control: CongestionControl) -> Self {
Self {
Expand All @@ -103,7 +104,7 @@ impl<T> QoSBuilderTrait for PublicationBuilder<PublisherBuilder<'_, '_>, T> {
}
}

impl<T> PublicationBuilder<PublisherBuilder<'_, '_>, T> {
impl<Kind> PublicationBuilder<PublisherBuilder<'_, '_>, Kind> {
/// Restrict the matching subscribers that will receive the published data
/// to the ones that have the given [`Locality`](crate::prelude::Locality).
#[zenoh_macros::unstable]
Expand All @@ -114,7 +115,7 @@ impl<T> PublicationBuilder<PublisherBuilder<'_, '_>, T> {
}
}

impl<P> ValueBuilderTrait for PublicationBuilder<P, PublicationBuilderPut> {
impl<P, Payload> EncodingBuilderTrait for PublicationBuilder<P, PublicationBuilderPut<Payload>> {
fn encoding<T: Into<Encoding>>(self, encoding: T) -> Self {
Self {
kind: PublicationBuilderPut {
Expand All @@ -124,29 +125,19 @@ impl<P> ValueBuilderTrait for PublicationBuilder<P, PublicationBuilderPut> {
..self
}
}
}

fn payload<IntoPayload>(self, payload: IntoPayload) -> Self
where
IntoPayload: Into<ZBytes>,
{
Self {
kind: PublicationBuilderPut {
payload: payload.into(),
..self.kind
},
..self
}
}
fn value<T: Into<Value>>(self, value: T) -> Self {
let Value { payload, encoding } = value.into();
Self {
kind: PublicationBuilderPut { payload, encoding },
..self
}
impl<P, Payload> AutoEncodingBuilderTrait<Payload>
for PublicationBuilder<P, PublicationBuilderPut<Payload>>
where
Payload: AutoEncoding,
{
fn get_payload(&self) -> Option<&Payload> {
Some(&self.kind.payload)
}
}

impl<P, T> SampleBuilderTrait for PublicationBuilder<P, T> {
impl<P, Kind> SampleBuilderTrait for PublicationBuilder<P, Kind> {
#[cfg(feature = "unstable")]
fn source_info(self, source_info: SourceInfo) -> Self {
Self {
Expand All @@ -163,7 +154,7 @@ impl<P, T> SampleBuilderTrait for PublicationBuilder<P, T> {
}
}

impl<P, T> TimestampBuilderTrait for PublicationBuilder<P, T> {
impl<P, Kind> TimestampBuilderTrait for PublicationBuilder<P, Kind> {
fn timestamp<TS: Into<Option<uhlc::Timestamp>>>(self, timestamp: TS) -> Self {
Self {
timestamp: timestamp.into(),
Expand All @@ -172,16 +163,19 @@ impl<P, T> TimestampBuilderTrait for PublicationBuilder<P, T> {
}
}

impl<P, T> Resolvable for PublicationBuilder<P, T> {
impl<P, Kind> Resolvable for PublicationBuilder<P, Kind> {
type To = ZResult<()>;
}

impl Wait for PublicationBuilder<PublisherBuilder<'_, '_>, PublicationBuilderPut> {
impl<Payload> Wait for PublicationBuilder<PublisherBuilder<'_, '_>, PublicationBuilderPut<Payload>>
where
Payload: Into<ZBytes>,
{
#[inline]
fn wait(self) -> <Self as Resolvable>::To {
let publisher = self.publisher.create_one_shot_publisher()?;
publisher.resolve_put(
self.kind.payload,
self.kind.payload.into(),
SampleKind::Put,
self.kind.encoding,
self.timestamp,
Expand All @@ -208,7 +202,11 @@ impl Wait for PublicationBuilder<PublisherBuilder<'_, '_>, PublicationBuilderDel
}
}

impl IntoFuture for PublicationBuilder<PublisherBuilder<'_, '_>, PublicationBuilderPut> {
impl<Payload> IntoFuture
for PublicationBuilder<PublisherBuilder<'_, '_>, PublicationBuilderPut<Payload>>
where
Payload: Into<ZBytes>,
{
type Output = <Self as Resolvable>::To;
type IntoFuture = Ready<<Self as Resolvable>::To>;

Expand Down Expand Up @@ -378,10 +376,13 @@ impl<'a, 'b> IntoFuture for PublisherBuilder<'a, 'b> {
}
}

impl Wait for PublicationBuilder<&Publisher<'_>, PublicationBuilderPut> {
impl<Payload> Wait for PublicationBuilder<&Publisher<'_>, PublicationBuilderPut<Payload>>
where
Payload: Into<ZBytes>,
{
fn wait(self) -> <Self as Resolvable>::To {
self.publisher.resolve_put(
self.kind.payload,
self.kind.payload.into(),
SampleKind::Put,
self.kind.encoding,
self.timestamp,
Expand All @@ -406,7 +407,10 @@ impl Wait for PublicationBuilder<&Publisher<'_>, PublicationBuilderDelete> {
}
}

impl IntoFuture for PublicationBuilder<&Publisher<'_>, PublicationBuilderPut> {
impl<Payload> IntoFuture for PublicationBuilder<&Publisher<'_>, PublicationBuilderPut<Payload>>
where
Payload: Into<ZBytes>,
{
type Output = <Self as Resolvable>::To;
type IntoFuture = Ready<<Self as Resolvable>::To>;

Expand Down
63 changes: 20 additions & 43 deletions zenoh/src/api/builders/sample.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,18 @@ use uhlc::Timestamp;
use zenoh_core::zresult;
use zenoh_protocol::core::CongestionControl;

use crate::api::{
bytes::{OptionZBytes, ZBytes},
encoding::Encoding,
key_expr::KeyExpr,
publisher::Priority,
sample::{QoS, QoSBuilder, Sample, SampleKind},
value::Value,
};
#[cfg(feature = "unstable")]
use crate::sample::SourceInfo;
use crate::{
api::{
bytes::{OptionZBytes, ZBytes},
encoding::Encoding,
key_expr::KeyExpr,
publisher::Priority,
sample::{QoS, QoSBuilder, Sample, SampleKind},
},
encoding::EncodingBuilderTrait,
};

pub trait QoSBuilderTrait {
/// Change the `congestion_control` to apply when routing the data.
Expand All @@ -52,16 +54,6 @@ pub trait SampleBuilderTrait {
fn attachment<T: Into<OptionZBytes>>(self, attachment: T) -> Self;
}

pub trait ValueBuilderTrait {
/// Set the [`Encoding`]
fn encoding<T: Into<Encoding>>(self, encoding: T) -> Self;
/// Sets the payload
fn payload<T: Into<ZBytes>>(self, payload: T) -> Self;
/// Sets both payload and encoding at once.
/// This is convenient for passing user type which supports `Into<Value>` when both payload and encoding depends on user type
fn value<T: Into<Value>>(self, value: T) -> Self;
}

#[derive(Clone, Debug)]
pub struct SampleBuilderPut;
#[derive(Clone, Debug)]
Expand All @@ -76,10 +68,7 @@ pub struct SampleBuilder<T> {
}

impl SampleBuilder<SampleBuilderPut> {
pub fn put<IntoKeyExpr, IntoZBytes>(
key_expr: IntoKeyExpr,
payload: IntoZBytes,
) -> SampleBuilder<SampleBuilderPut>
pub fn put<IntoKeyExpr, IntoZBytes>(key_expr: IntoKeyExpr, payload: IntoZBytes) -> Self
where
IntoKeyExpr: Into<KeyExpr<'static>>,
IntoZBytes: Into<ZBytes>,
Expand All @@ -99,6 +88,14 @@ impl SampleBuilder<SampleBuilderPut> {
_t: PhantomData::<SampleBuilderPut>,
}
}

pub fn payload<IntoZBytes>(mut self, payload: IntoZBytes) -> Self
where
IntoZBytes: Into<ZBytes>,
{
self.sample.payload = payload.into();
self
}
}

impl SampleBuilder<SampleBuilderDelete> {
Expand Down Expand Up @@ -210,7 +207,7 @@ impl<T> QoSBuilderTrait for SampleBuilder<T> {
}
}

impl ValueBuilderTrait for SampleBuilder<SampleBuilderPut> {
impl EncodingBuilderTrait for SampleBuilder<SampleBuilderPut> {
fn encoding<T: Into<Encoding>>(self, encoding: T) -> Self {
Self {
sample: Sample {
Expand All @@ -220,26 +217,6 @@ impl ValueBuilderTrait for SampleBuilder<SampleBuilderPut> {
_t: PhantomData::<SampleBuilderPut>,
}
}
fn payload<T: Into<ZBytes>>(self, payload: T) -> Self {
Self {
sample: Sample {
payload: payload.into(),
..self.sample
},
_t: PhantomData::<SampleBuilderPut>,
}
}
fn value<T: Into<Value>>(self, value: T) -> Self {
let Value { payload, encoding } = value.into();
Self {
sample: Sample {
payload,
encoding,
..self.sample
},
_t: PhantomData::<SampleBuilderPut>,
}
}
}

impl From<Sample> for SampleBuilder<SampleBuilderAny> {
Expand Down
Loading

0 comments on commit 08c207e

Please sign in to comment.