Use option instead of result<t, dropped>

This commit is contained in:
Aode (Lion) 2022-02-25 12:04:29 -06:00
parent 716f282c1d
commit 701f9cf0a4

View file

@ -24,8 +24,6 @@ pub struct Receiver<T> {
last_seen: usize,
}
pub struct Dropped;
fn main() -> Result<(), Box<dyn std::error::Error>> {
jive::block_on(async move {
let (tx, rx) = watch("meowdy".into());
@ -43,7 +41,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
for i in 0..5 {
let mut rx = rx.clone();
handles.push(jive::spawn(async move {
while let Ok(item) = rx.recv().await {
while let Some(item) = rx.recv().await {
println!("{} Got: {}", i, item);
}
}));
@ -69,7 +67,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
for i in 0..5 {
let mut rx = rx.clone();
handles.push(std::thread::spawn(move || {
while let Ok(item) = rx.recv_blocking() {
while let Some(item) = rx.recv_blocking() {
println!("{} Got: {}", i, item);
}
}));
@ -140,34 +138,34 @@ impl<T> Receiver<T> {
Some(self.state.item.lock().unwrap().clone())
}
pub async fn recv(&mut self) -> Result<T, Dropped>
pub async fn recv(&mut self) -> Option<T>
where
T: Clone,
{
loop {
if let Some(item) = self.try_recv() {
return Ok(item);
return Some(item);
}
if self.is_closed() {
return Err(Dropped);
return None;
}
self.listener.listen().await;
}
}
pub fn recv_blocking(&mut self) -> Result<T, Dropped>
pub fn recv_blocking(&mut self) -> Option<T>
where
T: Clone,
{
loop {
if let Some(item) = self.try_recv() {
return Ok(item);
return Some(item);
}
if self.is_closed() {
return Err(Dropped);
return None;
}
self.listener.listen_blocking();