Add test and update interface
This commit is contained in:
		
							parent
							
								
									c8384a95c5
								
							
						
					
					
						commit
						4623cea844
					
				| @ -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"] | ||||
|  | ||||
							
								
								
									
										62
									
								
								src/lib.rs
									
									
									
									
									
								
							
							
						
						
									
										62
									
								
								src/lib.rs
									
									
									
									
									
								
							| @ -4,6 +4,14 @@ pub trait Message: 'static + Clone + Send + Sync {} | ||||
| 
 | ||||
| impl<T> 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<T: Message>(&mut self) -> Subscription<T> { | ||||
|     pub async fn subscribe<T: Message>(&mut self) -> Result<Subscription<T>, SubscribeError> { | ||||
|         todo!() | ||||
|     } | ||||
| 
 | ||||
|     pub fn advertise<T: Message>(&mut self) -> Publication<T> { | ||||
|     pub async fn advertise<T: Message>(&mut self) -> Result<Publication<T>, AdvertiseError> { | ||||
|         todo!() | ||||
|     } | ||||
| 
 | ||||
|     pub fn subscribe_blocking<T: Message>(&mut self) -> Result<Subscription<T>, SubscribeError> { | ||||
|         todo!() | ||||
|     } | ||||
| 
 | ||||
|     pub fn advertise_blocking<T: Message>(&mut self) -> Result<Publication<T>, 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<T> { | ||||
| } | ||||
| 
 | ||||
| impl<T: Message> Subscription<T> { | ||||
|     pub async fn recv_wait(&mut self) -> Result<T, RecvErr> { | ||||
|     pub async fn recv(&mut self) -> Result<T, RecvError> { | ||||
|         todo!() | ||||
|     } | ||||
| 
 | ||||
|     pub fn recv(&mut self) -> Result<T, RecvErr> { | ||||
|     pub fn recv_blocking(&mut self) -> Result<T, RecvError> { | ||||
|         todo!() | ||||
|     } | ||||
| 
 | ||||
|     pub fn try_recv(&mut self) -> Result<T, RecvErr> { | ||||
|     pub fn try_recv(&mut self) -> Result<T, RecvError> { | ||||
|         todo!() | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| #[derive(Clone, Debug)] | ||||
| #[derive(Clone, Debug, PartialEq)] | ||||
| #[non_exhaustive] | ||||
| pub enum SendErr { | ||||
| pub enum SendError { | ||||
|     NoListeners, | ||||
| } | ||||
| 
 | ||||
| @ -57,7 +73,33 @@ pub struct Publication<T> { | ||||
| } | ||||
| 
 | ||||
| impl<T: Message> Publication<T> { | ||||
|     pub fn send(&mut self) -> Result<T, SendErr> { | ||||
|     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::<TestMsg>().unwrap(); | ||||
|         let mut publ = orsb.advertise_blocking::<TestMsg>().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)); | ||||
|     } | ||||
| } | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user