98 lines
2.8 KiB
Rust
98 lines
2.8 KiB
Rust
/*
|
|
* Copyright (C) 2023 - 2024:
|
|
* The Trinitrix Project <soispha@vhack.eu, antifallobst@systemausfall.org>
|
|
*
|
|
* This file is part of the Trixy crate for Trinitrix.
|
|
*
|
|
* Trixy is free software: you can redistribute it and/or modify
|
|
* it under the terms of the Lesser GNU General Public License as
|
|
* published by the Free Software Foundation, either version 3 of
|
|
* the License, or (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* and the Lesser GNU General Public License along with this program.
|
|
* If not, see <https://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
pub mod types {
|
|
pub use trixy_types::*;
|
|
}
|
|
|
|
pub mod macros {
|
|
pub use trixy_macros::*;
|
|
}
|
|
|
|
pub mod oneshot {
|
|
//! This module provides a synced version of tokio's oneshot channel.
|
|
//! Notably this is implemented as wrapper around std's mpsc channel, which closes after one received value
|
|
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)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|