2023-12-26 11:18:58 +00:00
|
|
|
pub mod types {
|
2023-12-26 11:14:08 +00:00
|
|
|
pub use trixy_types::*;
|
|
|
|
}
|
2023-12-25 21:23:52 +00:00
|
|
|
|
2023-12-26 11:18:58 +00:00
|
|
|
pub mod macros {
|
2023-12-26 11:14:08 +00:00
|
|
|
pub use trixy_macros::*;
|
2023-12-25 21:23:52 +00:00
|
|
|
}
|
2023-12-28 09:28:58 +00:00
|
|
|
|
|
|
|
pub mod oneshot {
|
|
|
|
use std::{
|
|
|
|
mem,
|
|
|
|
sync::mpsc::{self, RecvError, SendError, TryRecvError},
|
|
|
|
};
|
|
|
|
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub struct Sender<T> {
|
|
|
|
channel: mpsc::Sender<T>,
|
|
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct Receiver<T> {
|
|
|
|
channel: mpsc::Receiver<T>,
|
|
|
|
last_value: Option<T>,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn channel<T>() -> (Sender<T>, Receiver<T>) {
|
|
|
|
let (tx, rx) = mpsc::channel();
|
|
|
|
(
|
|
|
|
Sender { channel: tx },
|
|
|
|
Receiver {
|
|
|
|
channel: rx,
|
|
|
|
last_value: None,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
impl<T> Sender<T> {
|
|
|
|
pub fn send(&self, input: T) -> Result<(), SendError<T>> {
|
|
|
|
self.channel.send(input)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl<T> Receiver<T> {
|
|
|
|
pub fn try_recv(&mut self) -> Result<bool, TryRecvError> {
|
|
|
|
match self.channel.try_recv() {
|
|
|
|
Ok(ok) => {
|
|
|
|
self.close();
|
|
|
|
self.last_value = Some(ok);
|
|
|
|
Ok(true)
|
|
|
|
}
|
|
|
|
Err(err) => Err(err),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn close(&mut self) {
|
|
|
|
let (_, recv) = mpsc::channel();
|
|
|
|
let channel = mem::replace(&mut self.channel, recv);
|
|
|
|
drop(channel);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn recv(mut self) -> Result<T, RecvError> {
|
|
|
|
match self.channel.recv() {
|
|
|
|
Ok(ok) => {
|
|
|
|
self.close();
|
|
|
|
Ok(ok)
|
|
|
|
}
|
|
|
|
Err(err) => {
|
|
|
|
if let Some(val) = self.last_value {
|
|
|
|
Ok(val)
|
|
|
|
} else {
|
|
|
|
Err(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|