Last active
February 11, 2023 22:27
-
-
Save matthewjberger/32cbf96472dee8b0ddbcd1774d7fdec6 to your computer and use it in GitHub Desktop.
Async winit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[package] | |
name = "window" | |
version = "0.1.0" | |
edition = "2021" | |
[dependencies] | |
winit = "0.28.1" | |
async-std = "1.10.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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