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