Skip to main content

compio_actor/mailbox/
error.rs

1use std::{error::Error, fmt};
2
3use super::Call;
4use crate::Message;
5
6/// A message rejected by a mailbox or broker.
7#[derive(Debug, PartialEq, Eq)]
8pub enum DeliverError<M: Message> {
9    /// The mailbox is at capacity.
10    Full(M),
11    /// The actor is stopping or has exited.
12    Closed(M),
13}
14
15impl<M: Message> DeliverError<M> {
16    /// Recovers the rejected message.
17    pub fn into_inner(self) -> M {
18        match self {
19            Self::Full(message) | Self::Closed(message) => message,
20        }
21    }
22}
23
24impl<M: Message> fmt::Display for DeliverError<M> {
25    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
26        match self {
27            Self::Full(_) => f.write_str("actor mailbox is full"),
28            Self::Closed(_) => f.write_str("actor mailbox is closed"),
29        }
30    }
31}
32
33impl<M: Message + fmt::Debug> Error for DeliverError<M> {}
34
35/// A call that could not be delivered or answered.
36#[derive(Debug, PartialEq, Eq)]
37pub enum CallError<M: Message> {
38    /// The mailbox was at capacity.
39    Full(M),
40    /// The actor was stopping or had exited.
41    Closed(M),
42    /// The actor handled the request without replying.
43    NoReply,
44}
45
46impl<M: Message> CallError<M> {
47    pub(super) fn from_deliver<R: Message>(error: DeliverError<Call<M, R>>) -> Self {
48        match error {
49            DeliverError::Full(call) => Self::Full(call.into_message()),
50            DeliverError::Closed(call) => Self::Closed(call.into_message()),
51        }
52    }
53
54    /// Recovers a request that was not delivered.
55    pub fn into_inner(self) -> Option<M> {
56        match self {
57            Self::Full(message) | Self::Closed(message) => Some(message),
58            Self::NoReply => None,
59        }
60    }
61}
62
63impl<M: Message> fmt::Display for CallError<M> {
64    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
65        match self {
66            Self::Full(_) => f.write_str("actor mailbox is full"),
67            Self::Closed(_) => f.write_str("actor mailbox is closed"),
68            Self::NoReply => f.write_str("actor did not reply"),
69        }
70    }
71}
72
73impl<M: Message + fmt::Debug> Error for CallError<M> {}