use std::time::Duration;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum MailboxKind {
#[default]
Unbounded,
Bounded,
UnboundedDeque,
UnboundedPriority,
UnboundedStablePriority,
UnboundedControlAware,
BoundedControlAware,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum OverflowStrategy {
#[default]
DropNew,
DropHead,
DropTail,
Fail,
}
#[derive(Debug, Clone)]
pub struct MailboxConfig {
pub kind: MailboxKind,
pub capacity: usize,
pub push_timeout: Duration,
pub overflow: OverflowStrategy,
}
impl Default for MailboxConfig {
fn default() -> Self {
Self {
kind: MailboxKind::Unbounded,
capacity: 1_000,
push_timeout: Duration::from_secs(10),
overflow: OverflowStrategy::DropNew,
}
}
}
#[derive(Debug, Clone, Default)]
pub struct Mailbox {
pub config: MailboxConfig,
}
impl Mailbox {
pub fn new(config: MailboxConfig) -> Self {
Self { config }
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn default_mailbox_kind_unbounded() {
assert_eq!(Mailbox::default().config.kind, MailboxKind::Unbounded);
}
#[test]
fn default_overflow_drops_new() {
assert_eq!(MailboxConfig::default().overflow, OverflowStrategy::DropNew);
}
#[test]
fn config_for_each_kind_is_constructible() {
for k in [
MailboxKind::Unbounded,
MailboxKind::Bounded,
MailboxKind::UnboundedDeque,
MailboxKind::UnboundedPriority,
MailboxKind::UnboundedStablePriority,
MailboxKind::UnboundedControlAware,
MailboxKind::BoundedControlAware,
] {
let c = MailboxConfig { kind: k, ..Default::default() };
assert_eq!(c.kind, k);
}
}
}