use std::future::{IntoFuture, Ready};
#[cfg(feature = "shared-memory")]
use std::sync::Arc;
use zenoh_core::{Resolvable, Wait};
#[cfg(feature = "internal")]
use zenoh_keyexpr::OwnedKeyExpr;
use zenoh_result::ZResult;
#[cfg(feature = "shared-memory")]
use zenoh_shm::api::client_storage::ShmClientStorage;
use crate::api::session::Session;
#[cfg(feature = "internal")]
use crate::net::runtime::DynamicRuntime;
#[must_use = "Resolvables do nothing unless you resolve them using `.await` or `zenoh::Wait::wait`"]
pub struct OpenBuilder<TryIntoConfig>
where
TryIntoConfig: std::convert::TryInto<crate::config::Config> + Send + 'static,
<TryIntoConfig as std::convert::TryInto<crate::config::Config>>::Error: std::fmt::Debug,
{
config: TryIntoConfig,
#[cfg(feature = "shared-memory")]
shm_clients: Option<Arc<ShmClientStorage>>,
}
impl<TryIntoConfig> OpenBuilder<TryIntoConfig>
where
TryIntoConfig: std::convert::TryInto<crate::config::Config> + Send + 'static,
<TryIntoConfig as std::convert::TryInto<crate::config::Config>>::Error: std::fmt::Debug,
{
pub(crate) fn new(config: TryIntoConfig) -> Self {
Self {
config,
#[cfg(feature = "shared-memory")]
shm_clients: None,
}
}
}
#[cfg(feature = "shared-memory")]
impl<TryIntoConfig> OpenBuilder<TryIntoConfig>
where
TryIntoConfig: std::convert::TryInto<crate::config::Config> + Send + 'static,
<TryIntoConfig as std::convert::TryInto<crate::config::Config>>::Error: std::fmt::Debug,
{
pub fn with_shm_clients(mut self, shm_clients: Arc<ShmClientStorage>) -> Self {
self.shm_clients = Some(shm_clients);
self
}
}
impl<TryIntoConfig> Resolvable for OpenBuilder<TryIntoConfig>
where
TryIntoConfig: std::convert::TryInto<crate::config::Config> + Send + 'static,
<TryIntoConfig as std::convert::TryInto<crate::config::Config>>::Error: std::fmt::Debug,
{
type To = ZResult<Session>;
}
impl<TryIntoConfig> Wait for OpenBuilder<TryIntoConfig>
where
TryIntoConfig: std::convert::TryInto<crate::config::Config> + Send + 'static,
<TryIntoConfig as std::convert::TryInto<crate::config::Config>>::Error: std::fmt::Debug,
{
fn wait(self) -> <Self as Resolvable>::To {
let config: crate::config::Config = self
.config
.try_into()
.map_err(|e| zerror!("Invalid Zenoh configuration {:?}", &e))?;
Session::new(
config,
#[cfg(feature = "shared-memory")]
self.shm_clients,
)
.wait()
}
}
impl<TryIntoConfig> IntoFuture for OpenBuilder<TryIntoConfig>
where
TryIntoConfig: std::convert::TryInto<crate::config::Config> + Send + 'static,
<TryIntoConfig as std::convert::TryInto<crate::config::Config>>::Error: std::fmt::Debug,
{
type Output = <Self as Resolvable>::To;
type IntoFuture = Ready<<Self as Resolvable>::To>;
fn into_future(self) -> Self::IntoFuture {
std::future::ready(self.wait())
}
}
#[zenoh_macros::internal]
pub fn init(runtime: DynamicRuntime) -> InitBuilder {
InitBuilder {
runtime,
aggregated_subscribers: vec![],
aggregated_publishers: vec![],
}
}
#[must_use = "Resolvables do nothing unless you resolve them using `.await` or `zenoh::Wait::wait`"]
#[doc(hidden)]
#[zenoh_macros::internal]
pub struct InitBuilder {
runtime: DynamicRuntime,
aggregated_subscribers: Vec<OwnedKeyExpr>,
aggregated_publishers: Vec<OwnedKeyExpr>,
}
#[zenoh_macros::internal]
impl InitBuilder {
#[inline]
pub fn aggregated_subscribers(mut self, exprs: Vec<OwnedKeyExpr>) -> Self {
self.aggregated_subscribers = exprs;
self
}
#[inline]
pub fn aggregated_publishers(mut self, exprs: Vec<OwnedKeyExpr>) -> Self {
self.aggregated_publishers = exprs;
self
}
}
#[zenoh_macros::internal]
impl Resolvable for InitBuilder {
type To = ZResult<Session>;
}
#[zenoh_macros::internal]
impl Wait for InitBuilder {
fn wait(self) -> <Self as Resolvable>::To {
Ok(Session::init(
self.runtime.into(),
self.aggregated_subscribers,
self.aggregated_publishers,
)
.wait())
}
}
#[zenoh_macros::internal]
impl IntoFuture for InitBuilder {
type Output = <Self as Resolvable>::To;
type IntoFuture = Ready<<Self as Resolvable>::To>;
fn into_future(self) -> Self::IntoFuture {
std::future::ready(self.wait())
}
}