diff --git a/Cargo.toml b/Cargo.toml index 5ec3276..17b89af 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,3 +7,11 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] + +[dependencies.tokio] +version = "1" +features = ["sync"] + +[dev-dependencies.tokio] +version = "1" +features = ["rt", "macros"] diff --git a/src/lib.rs b/src/lib.rs index b975326..5083abf 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,6 +4,14 @@ pub trait Message: 'static + Clone + Send + Sync {} impl Message for T where T: 'static + Clone + Send + Sync {} +#[derive(Clone, Debug, PartialEq)] +#[non_exhaustive] +pub enum SubscribeError {} + +#[derive(Clone, Debug, PartialEq)] +#[non_exhaustive] +pub enum AdvertiseError {} + pub struct Orsb {} impl Orsb { @@ -11,18 +19,26 @@ impl Orsb { todo!() } - pub fn subscribe(&mut self) -> Subscription { + pub async fn subscribe(&mut self) -> Result, SubscribeError> { todo!() } - pub fn advertise(&mut self) -> Publication { + pub async fn advertise(&mut self) -> Result, AdvertiseError> { + todo!() + } + + pub fn subscribe_blocking(&mut self) -> Result, SubscribeError> { + todo!() + } + + pub fn advertise_blocking(&mut self) -> Result, AdvertiseError> { todo!() } } -#[derive(Clone, Debug)] +#[derive(Clone, Debug, PartialEq)] #[non_exhaustive] -pub enum RecvErr { +pub enum RecvError { Empty, Closed, Lagged, @@ -33,22 +49,22 @@ pub struct Subscription { } impl Subscription { - pub async fn recv_wait(&mut self) -> Result { + pub async fn recv(&mut self) -> Result { todo!() } - pub fn recv(&mut self) -> Result { + pub fn recv_blocking(&mut self) -> Result { todo!() } - pub fn try_recv(&mut self) -> Result { + pub fn try_recv(&mut self) -> Result { todo!() } } -#[derive(Clone, Debug)] +#[derive(Clone, Debug, PartialEq)] #[non_exhaustive] -pub enum SendErr { +pub enum SendError { NoListeners, } @@ -57,7 +73,33 @@ pub struct Publication { } impl Publication { - pub fn send(&mut self) -> Result { + pub fn send(&mut self, message: T) -> Result<(), SendError> { + let _ = message; todo!() } } + +#[cfg(test)] +mod test { + use super::*; + + #[derive(Clone, Debug, PartialEq)] + struct TestMsg(u8); + + #[test] + fn test_sync() { + let mut orsb = Orsb::new(); + + let mut sub = orsb.subscribe_blocking::().unwrap(); + let mut publ = orsb.advertise_blocking::().unwrap(); + + publ.send(TestMsg(10)).unwrap(); + publ.send(TestMsg(20)).unwrap(); + publ.send(TestMsg(30)).unwrap(); + + assert_eq!(sub.recv_blocking(), Ok(TestMsg(10))); + assert_eq!(sub.recv_blocking(), Ok(TestMsg(20))); + assert_eq!(sub.recv_blocking(), Ok(TestMsg(30))); + assert_eq!(sub.try_recv(), Err(RecvError::Empty)); + } +}