Skip to content

Instantly share code, notes, and snippets.

@matthewjberger
Last active February 11, 2023 22:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matthewjberger/32cbf96472dee8b0ddbcd1774d7fdec6 to your computer and use it in GitHub Desktop.
Save matthewjberger/32cbf96472dee8b0ddbcd1774d7fdec6 to your computer and use it in GitHub Desktop.
Async winit
[package]
name = "window"
version = "0.1.0"
edition = "2021"
[dependencies]
winit = "0.28.1"
async-std = "1.10.0"
use std::time::Duration;
use async_std::task;
use winit::{event_loop::{EventLoop, ControlFlow}, window::WindowBuilder};
async fn run(event_loop: EventLoop<()>) {
let _window = WindowBuilder::new().build(&event_loop).unwrap();
event_loop.run(move |_event, _, control_flow| {
*control_flow = ControlFlow::Wait;
task::spawn(async move {
let result = calculate_async().await;
println!("Result of async function: {}", result);
});
});
}
async fn calculate_async() -> i32 {
task::sleep(Duration::from_secs(1)).await;
42
}
fn main() {
task::block_on(run(EventLoop::new()));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment