use std::future::{IntoFuture, Ready};
use zenoh_config::wrappers::Hello;
use zenoh_core::{Resolvable, Wait};
use zenoh_protocol::core::WhatAmIMatcher;
use zenoh_result::ZResult;
use crate::api::{
handlers::{locked, Callback, DefaultHandler, IntoHandler},
scouting::{_scout, Scout},
};
#[must_use = "Resolvables do nothing unless you resolve them using `.await` or `zenoh::Wait::wait`"]
#[derive(Debug)]
pub struct ScoutBuilder<Handler> {
pub(crate) what: WhatAmIMatcher,
pub(crate) config: ZResult<crate::config::Config>,
pub(crate) handler: Handler,
}
impl ScoutBuilder<DefaultHandler> {
#[inline]
pub fn callback<F>(self, callback: F) -> ScoutBuilder<Callback<Hello>>
where
F: Fn(Hello) + Send + Sync + 'static,
{
self.with(Callback::from(callback))
}
#[inline]
pub fn callback_mut<F>(self, callback: F) -> ScoutBuilder<Callback<Hello>>
where
F: FnMut(Hello) + Send + Sync + 'static,
{
self.callback(locked(callback))
}
#[inline]
pub fn with<Handler>(self, handler: Handler) -> ScoutBuilder<Handler>
where
Handler: IntoHandler<Hello>,
{
let ScoutBuilder {
what,
config,
handler: _,
} = self;
ScoutBuilder {
what,
config,
handler,
}
}
}
impl<Handler> Resolvable for ScoutBuilder<Handler>
where
Handler: IntoHandler<Hello> + Send,
Handler::Handler: Send,
{
type To = ZResult<Scout<Handler::Handler>>;
}
impl<Handler> Wait for ScoutBuilder<Handler>
where
Handler: IntoHandler<Hello> + Send,
Handler::Handler: Send,
{
fn wait(self) -> <Self as Resolvable>::To {
let (callback, receiver) = self.handler.into_handler();
_scout(self.what, self.config?, callback).map(|scout| Scout { scout, receiver })
}
}
impl<Handler> IntoFuture for ScoutBuilder<Handler>
where
Handler: IntoHandler<Hello> + Send,
Handler::Handler: Send,
{
type Output = <Self as Resolvable>::To;
type IntoFuture = Ready<<Self as Resolvable>::To>;
fn into_future(self) -> Self::IntoFuture {
std::future::ready(self.wait())
}
}