use crate::{
err::Error,
rctx::{RCtxState, ReplyContext}
};
pub struct QueueNode<S, R, E> {
pub(crate) msg: S,
pub(crate) reply: swctx::SetCtx<R, RCtxState, E>
}
#[repr(transparent)]
pub struct Server<S, R, E>(pub(crate) sigq::Puller<QueueNode<S, R, E>>);
impl<S, R, E> Server<S, R, E>
where
S: 'static + Send,
R: 'static + Send,
E: 'static + Send
{
pub fn wait(&self) -> Result<(S, ReplyContext<R, E>), Error<E>> {
let node = self.0.pop().map_err(|_| Error::ClientsDisappeared)?;
let msg = node.msg;
let rctx = ReplyContext::try_from(node.reply)?;
Ok((msg, rctx))
}
#[allow(clippy::type_complexity)]
pub fn try_pop(&self) -> Result<Option<(S, ReplyContext<R, E>)>, Error<E>> {
let node = self.0.try_pop().map_err(|_| Error::ClientsDisappeared)?;
if let Some(node) = node {
let msg = node.msg;
let rctx = ReplyContext::try_from(node.reply)?;
Ok(Some((msg, rctx)))
} else {
Ok(None)
}
}
#[allow(clippy::missing_errors_doc)]
pub async fn async_wait(&self) -> Result<(S, ReplyContext<R, E>), Error<E>> {
let node = self.0.apop().await.map_err(|_| Error::ClientsDisappeared)?;
let msg = node.msg;
let rctx = ReplyContext::try_from(node.reply)?;
Ok((msg, rctx))
}
#[must_use]
pub fn was_empty(&self) -> bool {
self.0.was_empty()
}
}