loader/src/lib.rs
2024-07-02 20:53:20 -04:00

19 lines
487 B
Rust

pub use std::sync::mpsc::TryRecvError;
use std::sync::mpsc::{self, Receiver};
pub struct Loader<T> {
rx: Receiver<T>,
}
impl<T: Send + 'static> Loader<T> {
pub fn new<F: FnOnce() -> T + Send + 'static>(f: F) -> Loader<T> {
let (tx, rx) = mpsc::sync_channel(0);
std::thread::spawn(move || tx.send(f()).unwrap());
return Loader { rx };
}
#[inline(always)]
pub fn try_recv(&self) -> Result<T, TryRecvError> {
self.rx.try_recv()
}
}