pub struct Work(Box<dyn FnOnce() + Send + 'static>);
impl Work {
pub fn new(f: impl FnOnce() + Send + 'static) -> Self {
Work(Box::new(f))
}
pub fn run(self) {
let Work(f) = self;
f();
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_it_runs() {
let make_me_true = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false));
let work = Work::new({
let make_me_true = make_me_true.clone();
move || {
make_me_true.store(true, std::sync::atomic::Ordering::SeqCst);
}
});
work.run();
assert!(make_me_true.load(std::sync::atomic::Ordering::SeqCst));
}
}