Skip to content

Commit

Permalink
feat: remove Value from the public API, make it internal for plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
wyfo committed Jun 11, 2024
1 parent 13ed78e commit 14082b0
Show file tree
Hide file tree
Showing 16 changed files with 93 additions and 147 deletions.
2 changes: 1 addition & 1 deletion plugins/zenoh-backend-example/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use std::collections::{hash_map::Entry, HashMap};

use async_std::sync::RwLock;
use async_trait::async_trait;
use zenoh::{key_expr::OwnedKeyExpr, time::Timestamp, value::Value};
use zenoh::{internal::Value, key_expr::OwnedKeyExpr, time::Timestamp};
use zenoh_backend_traits::{
config::{StorageConfig, VolumeConfig},
Capability, History, Persistence, Storage, StorageInsertionResult, StoredData, Volume,
Expand Down
2 changes: 1 addition & 1 deletion plugins/zenoh-backend-traits/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ async-std = { workspace = true, features = ["default"] }
async-trait = { workspace = true }
derive_more = { workspace = true }
serde_json = { workspace = true }
zenoh = { workspace = true, features = ["unstable"] }
zenoh = { workspace = true, features = ["unstable", "internal"] }
zenoh-result = { workspace = true }
zenoh-util = { workspace = true }
schemars = { workspace = true }
Expand Down
2 changes: 1 addition & 1 deletion plugins/zenoh-backend-traits/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,9 @@ use async_trait::async_trait;
use const_format::concatcp;
use zenoh::{
core::Result as ZResult,
internal::Value,
key_expr::{keyexpr, OwnedKeyExpr},
time::Timestamp,
value::Value,
};
use zenoh_plugin_trait::{PluginControl, PluginInstance, PluginStatusRec, StructVersion};
use zenoh_util::concat_enabled_features;
Expand Down
8 changes: 4 additions & 4 deletions plugins/zenoh-plugin-rest/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,16 @@ use zenoh::{
},
key_expr::{keyexpr, KeyExpr},
query::{QueryConsolidation, Reply},
sample::{Sample, SampleKind, ValueBuilderTrait},
sample::{EncodingBuilderTrait, Sample, SampleKind},
selector::{Selector, TIME_RANGE_KEY},
session::{Session, SessionDeclarations},
value::Value,
};
use zenoh_plugin_trait::{plugin_long_version, plugin_version, Plugin, PluginControl};
use zenoh_result::{bail, zerror, ZResult};

mod config;
pub use config::Config;
use zenoh::query::ReplyError;

const GIT_VERSION: &str = git_version::git_version!(prefix = "v", cargo_prefix = "v");
lazy_static::lazy_static! {
Expand Down Expand Up @@ -95,7 +95,7 @@ fn sample_to_json(sample: &Sample) -> JSONSample {
}
}

fn result_to_json(sample: Result<&Sample, &Value>) -> JSONSample {
fn result_to_json(sample: Result<&Sample, &ReplyError>) -> JSONSample {
match sample {
Ok(sample) => sample_to_json(sample),
Err(err) => JSONSample {
Expand Down Expand Up @@ -136,7 +136,7 @@ fn sample_to_html(sample: &Sample) -> String {
)
}

fn result_to_html(sample: Result<&Sample, &Value>) -> String {
fn result_to_html(sample: Result<&Sample, &ReplyError>) -> String {
match sample {
Ok(sample) => sample_to_html(sample),
Err(err) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use std::{collections::HashMap, sync::Arc};

use async_std::sync::RwLock;
use async_trait::async_trait;
use zenoh::{core::Result as ZResult, key_expr::OwnedKeyExpr, time::Timestamp, value::Value};
use zenoh::{core::Result as ZResult, internal::Value, key_expr::OwnedKeyExpr, time::Timestamp};
use zenoh_backend_traits::{
config::{StorageConfig, VolumeConfig},
*,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ use std::{

use async_std::sync::Arc;
use zenoh::{
key_expr::OwnedKeyExpr, prelude::*, sample::Sample, selector::Selector, time::Timestamp,
value::Value, Session,
internal::Value, key_expr::OwnedKeyExpr, prelude::*, sample::Sample, selector::Selector,
time::Timestamp, Session,
};

use super::{digest::*, Snapshotter};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ use std::{
use async_std::sync::{Arc, RwLock};
use flume::{Receiver, Sender};
use zenoh::{
internal::Value,
key_expr::{KeyExpr, OwnedKeyExpr},
prelude::*,
sample::{Sample, SampleBuilder},
selector::Selector,
time::Timestamp,
value::Value,
Session,
};

Expand Down
5 changes: 2 additions & 3 deletions plugins/zenoh-plugin-storage-manager/src/replica/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use zenoh::{
internal::{
bail,
buffers::{SplitBuffer, ZBuf},
zenoh_home, Timed, TimedEvent, Timer,
zenoh_home, Timed, TimedEvent, Timer, Value,
},
key_expr::{
keyexpr_tree::{
Expand All @@ -35,11 +35,10 @@ use zenoh::{
KeyExpr, OwnedKeyExpr,
},
query::{ConsolidationMode, QueryTarget},
sample::{Sample, SampleBuilder, SampleKind, TimestampBuilderTrait, ValueBuilderTrait},
sample::{EncodingBuilderTrait, Sample, SampleBuilder, SampleKind, TimestampBuilderTrait},
selector::Selector,
session::{Session, SessionDeclarations},
time::{new_timestamp, Timestamp, NTP64},
value::Value,
};
use zenoh_backend_traits::{
config::{GarbageCollectionConfig, StorageConfig},
Expand Down
25 changes: 2 additions & 23 deletions zenoh/src/api/builders/publisher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,14 @@ use zenoh_protocol::{core::CongestionControl, network::Mapping};
use crate::api::sample::SourceInfo;
use crate::api::{
builders::sample::{
QoSBuilderTrait, SampleBuilderTrait, TimestampBuilderTrait, ValueBuilderTrait,
EncodingBuilderTrait, QoSBuilderTrait, SampleBuilderTrait, TimestampBuilderTrait,
},
bytes::{OptionZBytes, ZBytes},
encoding::Encoding,
key_expr::KeyExpr,
publisher::{Priority, Publisher},
sample::{Locality, SampleKind},
session::SessionRef,
value::Value,
};

pub type SessionPutBuilder<'a, 'b> =
Expand Down Expand Up @@ -114,7 +113,7 @@ impl<T> PublicationBuilder<PublisherBuilder<'_, '_>, T> {
}
}

impl<P> ValueBuilderTrait for PublicationBuilder<P, PublicationBuilderPut> {
impl<P> EncodingBuilderTrait for PublicationBuilder<P, PublicationBuilderPut> {
fn encoding<T: Into<Encoding>>(self, encoding: T) -> Self {
Self {
kind: PublicationBuilderPut {
Expand All @@ -124,26 +123,6 @@ 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, T> SampleBuilderTrait for PublicationBuilder<P, T> {
Expand Down
38 changes: 10 additions & 28 deletions zenoh/src/api/builders/sample.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ use crate::api::{
key_expr::KeyExpr,
publisher::Priority,
sample::{QoS, QoSBuilder, Sample, SampleKind},
value::Value,
};
#[cfg(feature = "unstable")]
use crate::sample::SourceInfo;
Expand Down Expand Up @@ -52,14 +51,9 @@ pub trait SampleBuilderTrait {
fn attachment<T: Into<OptionZBytes>>(self, attachment: T) -> Self;
}

pub trait ValueBuilderTrait {
pub trait EncodingBuilderTrait {
/// 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)]
Expand Down Expand Up @@ -99,6 +93,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 +212,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 +222,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
79 changes: 55 additions & 24 deletions zenoh/src/api/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use zenoh_result::ZResult;
#[zenoh_macros::unstable]
use super::{builders::sample::SampleBuilderTrait, bytes::OptionZBytes, sample::SourceInfo};
use super::{
builders::sample::{QoSBuilderTrait, ValueBuilderTrait},
builders::sample::{EncodingBuilderTrait, QoSBuilderTrait},
bytes::ZBytes,
encoding::Encoding,
handlers::{locked, Callback, DefaultHandler, IntoHandler},
Expand Down Expand Up @@ -79,27 +79,63 @@ impl Default for QueryConsolidation {
}
}

/// Structs returned by a [`get`](Session::get).
/// Error returned by a [`get`](Session::get).
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ReplyError {
pub(crate) payload: ZBytes,
pub(crate) encoding: Encoding,
}

impl ReplyError {
/// Gets the payload of this ReplyError.
#[inline]
pub fn payload(&self) -> &ZBytes {
&self.payload
}

/// Gets the payload of this ReplyError.
#[inline]
pub fn payload_mut(&mut self) -> &mut ZBytes {
&mut self.payload
}

/// Gets the encoding of this ReplyError.
#[inline]
pub fn encoding(&self) -> &Encoding {
&self.encoding
}
}

impl From<Value> for ReplyError {
fn from(value: Value) -> Self {
Self {
payload: value.payload,
encoding: value.encoding,
}
}
}

/// Struct returned by a [`get`](Session::get).
#[non_exhaustive]
#[derive(Clone, Debug)]
pub struct Reply {
pub(crate) result: Result<Sample, Value>,
pub(crate) result: Result<Sample, ReplyError>,
pub(crate) replier_id: ZenohId,
}

impl Reply {
/// Gets the a borrowed result of this `Reply`. Use [`Reply::into_result`] to take ownership of the result.
pub fn result(&self) -> Result<&Sample, &Value> {
pub fn result(&self) -> Result<&Sample, &ReplyError> {
self.result.as_ref()
}

/// Gets the a mutable borrowed result of this `Reply`. Use [`Reply::into_result`] to take ownership of the result.
pub fn result_mut(&mut self) -> Result<&mut Sample, &mut Value> {
pub fn result_mut(&mut self) -> Result<&mut Sample, &mut ReplyError> {
self.result.as_mut()
}

/// Converts this `Reply` into the its result. Use [`Reply::result`] it you don't want to take ownership.
pub fn into_result(self) -> Result<Sample, Value> {
pub fn into_result(self) -> Result<Sample, ReplyError> {
self.result
}

Expand All @@ -109,7 +145,7 @@ impl Reply {
}
}

impl From<Reply> for Result<Sample, Value> {
impl From<Reply> for Result<Sample, ReplyError> {
fn from(value: Reply) -> Self {
value.into_result()
}
Expand Down Expand Up @@ -198,7 +234,7 @@ impl QoSBuilderTrait for SessionGetBuilder<'_, '_, DefaultHandler> {
}
}

impl<Handler> ValueBuilderTrait for SessionGetBuilder<'_, '_, Handler> {
impl<Handler> EncodingBuilderTrait for SessionGetBuilder<'_, '_, Handler> {
fn encoding<T: Into<Encoding>>(self, encoding: T) -> Self {
let mut value = self.value.unwrap_or_default();
value.encoding = encoding.into();
Expand All @@ -207,22 +243,6 @@ impl<Handler> ValueBuilderTrait for SessionGetBuilder<'_, '_, Handler> {
..self
}
}

fn payload<T: Into<ZBytes>>(self, payload: T) -> Self {
let mut value = self.value.unwrap_or_default();
value.payload = payload.into();
Self {
value: Some(value),
..self
}
}
fn value<T: Into<Value>>(self, value: T) -> Self {
let value: Value = value.into();
Self {
value: if value.is_empty() { None } else { Some(value) },
..self
}
}
}

impl<'a, 'b> SessionGetBuilder<'a, 'b, DefaultHandler> {
Expand Down Expand Up @@ -367,6 +387,17 @@ impl<'a, 'b> SessionGetBuilder<'a, 'b, DefaultHandler> {
}
}
impl<'a, 'b, Handler> SessionGetBuilder<'a, 'b, Handler> {
#[inline]
pub fn payload<IntoZBytes>(mut self, payload: IntoZBytes) -> Self
where
IntoZBytes: Into<ZBytes>,
{
let mut value = self.value.unwrap_or_default();
value.payload = payload.into();
self.value = Some(value);
self
}

/// Change the target of the query.
#[inline]
pub fn target(self, target: QueryTarget) -> Self {
Expand Down
Loading

0 comments on commit 14082b0

Please sign in to comment.