autocommit 2022-02-28 00:14:11 CET
This commit is contained in:
parent
db88f2db42
commit
0e9afcbfa5
22 changed files with 1013 additions and 239 deletions
38
src/utils/run_toplevel.rs
Normal file
38
src/utils/run_toplevel.rs
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
use crate::async_engine::SpawnedFuture;
|
||||
use crate::{AsyncEngine, AsyncQueue};
|
||||
use std::rc::Rc;
|
||||
|
||||
pub struct RunToplevelFuture {
|
||||
_future: SpawnedFuture<()>,
|
||||
}
|
||||
|
||||
pub struct RunToplevel {
|
||||
queue: AsyncQueue<Box<dyn FnOnce()>>,
|
||||
}
|
||||
|
||||
impl RunToplevel {
|
||||
pub fn install(eng: &Rc<AsyncEngine>) -> (RunToplevelFuture, Rc<RunToplevel>) {
|
||||
let slf = Rc::new(RunToplevel {
|
||||
queue: Default::default(),
|
||||
});
|
||||
let future = eng.spawn({
|
||||
let slf = slf.clone();
|
||||
async move {
|
||||
loop {
|
||||
let f = slf.queue.pop().await;
|
||||
f();
|
||||
}
|
||||
}
|
||||
});
|
||||
let future = RunToplevelFuture { _future: future };
|
||||
(future, slf)
|
||||
}
|
||||
|
||||
pub fn schedule<F: FnOnce() + 'static>(&self, f: F) {
|
||||
self.schedule_dyn(Box::new(f));
|
||||
}
|
||||
|
||||
fn schedule_dyn(&self, f: Box<dyn FnOnce()>) {
|
||||
self.queue.push(f);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue