use super::{Error, Message, Result};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct SinkId(u32);
impl SinkId {
pub const NONE: SinkId = SinkId(0);
pub fn try_new(id: u32) -> Result<Self> {
if id == 0 {
return Err(Error::config("SinkId 0 is reserved; use SinkId::NONE"));
}
Ok(SinkId(id))
}
pub const fn new(id: u32) -> Self {
assert!(id != 0, "SinkId 0 is reserved; use SinkId::NONE");
SinkId(id)
}
pub const fn as_u32(self) -> u32 {
self.0
}
pub const fn is_none(self) -> bool {
self.0 == 0
}
}
pub trait Placement: Send + Sync {
type Message: Message;
fn route(&mut self, message: &Self::Message) -> Result<SinkId>;
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn none_is_zero() {
assert_eq!(SinkId::NONE.as_u32(), 0);
assert!(SinkId::NONE.is_none());
}
#[test]
fn try_new_rejects_zero() {
assert!(SinkId::try_new(0).is_err());
assert_eq!(SinkId::try_new(1).unwrap().as_u32(), 1);
}
#[test]
#[should_panic]
fn new_panics_on_zero() {
let _ = SinkId::new(0);
}
}