mod executor {
use std::future::Future;
use std::sync::Arc;
use std::task::{Context, Poll, Wake};
use std::thread::{self, Thread};
struct ThreadWaker(Thread);
impl Wake for ThreadWaker {
fn wake(self: Arc<Self>) {
self.0.unpark();
}
}
pub fn block_on<T>(fut: impl Future<Output = T>) -> T {
let mut fut = Box::pin(fut);
let t = thread::current();
let waker = Arc::new(ThreadWaker(t)).into();
let mut cx = Context::from_waker(&waker);
loop {
match fut.as_mut().poll(&mut cx) {
Poll::Ready(res) => return res,
Poll::Pending => thread::park(),
}
}
}
}
#[test]
fn main() {
i_slint_backend_testing::init_with_event_loop();
slint::invoke_from_event_loop(|| {
let handle = slint::spawn_local(async { String::from("Hello") }).unwrap();
slint::spawn_local(async move { panic!("Aborted task") }).unwrap().abort();
let handle2 = slint::spawn_local(async move { handle.await + ", World" }).unwrap();
std::thread::spawn(move || {
let x = executor::block_on(handle2);
assert_eq!(x, "Hello, World");
slint::quit_event_loop().unwrap();
});
})
.unwrap();
slint::run_event_loop().unwrap();
}