use crate::{err::Error, server::QueueNode as ServerQueueNode};
use super::rctx::RCtxState;
#[repr(transparent)]
pub struct Client<S, R, E>(pub(crate) sigq::Pusher<ServerQueueNode<S, R, E>>);
impl<S, R, E> Client<S, R, E>
where
R: 'static + Send,
E: 'static + Send
{
pub fn req(&self, out: S) -> Result<R, Error<E>> {
let (sctx, wctx) = swctx::mkpair();
self
.0
.push(ServerQueueNode {
msg: out,
reply: sctx
})
.map_err(|_| Error::ServerDisappeared)?;
Ok(wctx.wait()?)
}
pub fn req_async(&self, out: S) -> Result<WaitReply<R, E>, Error<E>>
where
S: Send
{
let (sctx, wctx) = swctx::mkpair();
self
.0
.push(ServerQueueNode {
msg: out,
reply: sctx
})
.map_err(|_| Error::ServerDisappeared)?;
Ok(WaitReply(wctx))
}
#[allow(clippy::missing_errors_doc)]
#[deprecated(since = "0.10.2", note = "Use req() instead.")]
pub fn send(&self, out: S) -> Result<R, Error<E>> {
self.req(out)
}
#[allow(clippy::missing_errors_doc)]
pub async fn areq(&self, out: S) -> Result<R, Error<E>>
where
S: Send
{
let (sctx, wctx) = swctx::mkpair();
self
.0
.push(ServerQueueNode {
msg: out,
reply: sctx
})
.map_err(|_| Error::ServerDisappeared)?;
Ok(wctx.wait_async().await?)
}
#[deprecated(since = "0.10.2", note = "Use areq() instead.")]
#[allow(clippy::missing_errors_doc)]
pub async fn asend(&self, out: S) -> Result<R, Error<E>>
where
S: Send
{
self.areq(out).await
}
#[must_use]
pub fn weak(&self) -> Weak<S, R, E> {
Weak(self.0.weak())
}
}
impl<S, R, E> Clone for Client<S, R, E> {
fn clone(&self) -> Self {
Self(self.0.clone())
}
}
pub struct WaitReply<R, E>(swctx::WaitCtx<R, RCtxState, E>);
impl<R, E> WaitReply<R, E> {
pub fn wait(self) -> Result<R, Error<E>> {
Ok(self.0.wait()?)
}
#[allow(clippy::missing_errors_doc)]
pub async fn wait_async(self) -> Result<R, Error<E>>
where
R: Send,
E: Send
{
Ok(self.0.wait_async().await?)
}
}
#[repr(transparent)]
pub struct Weak<S, R, E>(
pub(crate) sigq::WeakPusher<ServerQueueNode<S, R, E>>
);
impl<S, R, E> Clone for Weak<S, R, E> {
fn clone(&self) -> Self {
Self(self.0.clone())
}
}
impl<S, R, E> Weak<S, R, E> {
#[must_use]
pub fn upgrade(&self) -> Option<Client<S, R, E>> {
self.0.upgrade().map(|x| Client(x))
}
}