Skip to content

Commit

Permalink
Merge pull request #12 from notgull/desync
Browse files Browse the repository at this point in the history
  • Loading branch information
notgull authored Aug 29, 2023
2 parents 916395e + 97bfc82 commit 7f5ba27
Show file tree
Hide file tree
Showing 24 changed files with 1,560 additions and 1,183 deletions.
26 changes: 22 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
strategy:
fail-fast: false
matrix:
rust_version: ['1.65.0', stable, nightly]
rust_version: ['1.67.1', stable, nightly]
platform:
# Note: Make sure that we test all the `docs.rs` targets defined in Cargo.toml!
- { target: x86_64-pc-windows-msvc, os: windows-latest, }
Expand All @@ -39,6 +39,12 @@ jobs:
- { target: x86_64-apple-darwin, os: macos-latest, }
- { target: x86_64-apple-ios, os: macos-latest, }
- { target: aarch64-apple-ios, os: macos-latest, }
exclude:
- rust_version: 1.67.1
platform: { target: aarch64-linux-android, os: ubuntu-latest, cmd: 'apk --', features: "android-native-activity" }
include:
- rust_version: 1.69.0
platform: { target: aarch64-linux-android, os: ubuntu-latest, cmd: 'apk --', features: "android-native-activity" }

env:
RUST_BACKTRACE: 1
Expand Down Expand Up @@ -68,9 +74,19 @@ jobs:
- name: Install GCC Multilib
if: (matrix.platform.os == 'ubuntu-latest') && contains(matrix.platform.target, 'i686')
run: sudo apt-get update && sudo apt-get install gcc-multilib
- name: Install cargo-apk

- name: Cache cargo-apk
if: contains(matrix.platform.target, 'android')
run: cargo install cargo-apk
id: cargo-apk-cache
uses: actions/cache@v3
with:
path: ~/.cargo/bin/cargo-apk
# Change this key if we update the required cargo-apk version
key: cargo-apk-${{ matrix.rust_version }}-${{ matrix.platform.name }}-v0-9-7

- name: Install cargo-apk
if: contains(matrix.platform.target, 'android') && (steps.cargo-apk-cache.outputs.cache-hit != 'true')
run: cargo install cargo-apk --version=^0.9.7 --locked

- name: Check documentation
shell: bash
Expand Down Expand Up @@ -99,7 +115,9 @@ jobs:
!contains(matrix.platform.target, 'redox') &&
!contains(matrix.platform.target, 'android') &&
matrix.rust_version != '1.64.0'
run: cargo $CMD test --verbose --target ${{ matrix.platform.target }} $OPTIONS --features $FEATURES
run: |
cargo $CMD test --verbose --target ${{ matrix.platform.target }} $OPTIONS --features $FEATURES
cargo $CMD test --verbose --target ${{ matrix.platform.target }} $OPTIONS --features "$FEATURES,thread_safe"
- name: Lint with clippy
shell: bash
Expand Down
13 changes: 7 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,33 @@ authors = ["John Nunley <[email protected]>"]
description = "Use winit like an async runtime"
repository = "https://github.com/notgull/async-winit"
license = "LGPL-3.0-or-later OR MPL-2.0"
rust-version = "1.67.1"

[dependencies]
async-broadcast = "0.5.1"
async-channel = "1.8.0"
async-lock = "2.7.0"
async-channel = { version = "1.8.0", optional = true }
cfg-if = "1.0.0"
concurrent-queue = "2.2.0"
event-listener = "2.5.3"
concurrent-queue = { version = "2.2.0", optional = true }
futures-lite = { version = "1.13.0", default-features = false }
once_cell = "1.17.1"
parking = "2.1.0"
pin-project-lite = "0.2.9"
raw-window-handle = "0.5.2"
slab = "0.4.8"
unsend = { version = "0.2.1", default-features = false, features = ["alloc"] }
winit = { version = "0.28.3", default-features = false }

[build-dependencies]
cfg_aliases = "0.1.1"

[dev-dependencies]
async-channel = "1.8.0"
futures-lite = { version = "1.13.0", features = ["std"], default-features = false }
softbuffer = { version = "0.2.0", default-features = false, features = ["x11"] }
winit = { version = "0.28.3", default-features = false, features = ["x11"] }

[features]
default = ["x11", "wayland", "wayland-dlopen"]
default = ["wayland", "wayland-dlopen", "x11"]
thread_safe = ["async-channel", "concurrent-queue"]
x11 = ["winit/x11"]
wayland = ["winit/wayland"]
wayland-dlopen = ["winit/wayland-dlopen"]
Expand Down
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,10 @@ This strategy is a bit long winded. Now, compare against the equivalent `async-w
```rust
use async_winit::event_loop::EventLoop;
use async_winit::window::Window;
use async_winit::ThreadUnsafe;
use futures_lite::prelude::*;

fn main2(evl: EventLoop) {
fn main2(evl: EventLoop<ThreadUnsafe>) {
let window_target = evl.window_target().clone();

evl.block_on(async move {
Expand All @@ -71,13 +72,13 @@ fn main2(evl: EventLoop) {
window_target.resumed().await;

// Create a window.
let window = Window::new().await.unwrap();
let window = Window::<ThreadUnsafe>::new().await.unwrap();

// Print the size of the window when it is resized.
let print_size = async {
window
.resized()
.wait_many()
.wait()
.for_each(|size| {
println!("{:?}", size);
})
Expand All @@ -88,14 +89,14 @@ fn main2(evl: EventLoop) {

// Wait until the window is closed.
let close = async {
window.close_requested().wait_once().await;
window.close_requested().wait().await;
println!("Close");
true
};

// Wait until the application is suspended.
let suspend = async {
window_target.suspended().wait_once().await;
window_target.suspended().wait().await;
false
};

Expand Down
4 changes: 2 additions & 2 deletions examples/timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Public License along with `async-winit`. If not, see <https://www.gnu.org/licens
use std::time::Duration;

use async_winit::event_loop::{EventLoop, EventLoopBuilder};
use async_winit::Timer;
use async_winit::{DefaultThreadSafety, Timer};

fn main() {
main2(EventLoopBuilder::new().build())
Expand All @@ -31,7 +31,7 @@ fn main2(evl: EventLoop) {
let target = evl.window_target().clone();
evl.block_on(async move {
// Wait one second.
Timer::after(Duration::from_secs(1)).await;
Timer::<DefaultThreadSafety>::after(Duration::from_secs(1)).await;

// Exit.
target.exit().await
Expand Down
16 changes: 7 additions & 9 deletions examples/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use std::time::Duration;

use async_winit::event_loop::{EventLoop, EventLoopBuilder};
use async_winit::window::Window;
use async_winit::Timer;
use async_winit::{DefaultThreadSafety, Timer};

use futures_lite::prelude::*;
use softbuffer::GraphicsContext;
Expand All @@ -38,18 +38,18 @@ fn main2(evl: EventLoop) {
target.resumed().await;

// Create a window.
let window = Window::new().await.unwrap();
let window = Window::<DefaultThreadSafety>::new().await.unwrap();

// Print resize events.
let print_resize = {
window.resized().wait_many().for_each(|new_size| {
window.resized().wait().for_each(|new_size| {
println!("Window resized to {:?}", new_size);
})
};

// Print the position every second.
let print_position = {
Timer::interval(Duration::from_secs(1))
Timer::<DefaultThreadSafety>::interval(Duration::from_secs(1))
.then(|_| window.inner_position())
.for_each(|posn| {
println!("Window position: {:?}", posn);
Expand All @@ -63,10 +63,10 @@ fn main2(evl: EventLoop) {
let mut buf = vec![];

async move {
let mut waiter = window.redraw_requested().wait_guard();
let mut waiter = window.redraw_requested().wait();

loop {
let _guard = waiter.wait().await;
let _guard = waiter.hold().await;
let inner_size = window.inner_size().await;

// Get the softbuffer.
Expand All @@ -92,9 +92,7 @@ fn main2(evl: EventLoop) {
};

// Wait for the window to close.
window
.close_requested()
.wait_once()
async { window.close_requested().wait().await }
.or(print_resize)
.or(print_position)
.or(draw)
Expand Down
25 changes: 13 additions & 12 deletions smol_example/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use async_winit::dpi::PhysicalSize;
use async_winit::event_loop::EventLoop;
use async_winit::window::Window;
use async_winit::ThreadUnsafe;

use color_eyre::eyre::{bail, eyre, Context, Error, Result};

Expand All @@ -27,7 +28,7 @@ fn main() {
main2(EventLoop::new())
}

fn main2(event_loop: EventLoop) {
fn main2(event_loop: EventLoop<ThreadUnsafe>) {
let target = event_loop.window_target().clone();

event_loop.block_on(async move {
Expand Down Expand Up @@ -76,20 +77,20 @@ fn main2(event_loop: EventLoop) {

loop {
// Wait for the application to become resumed, poll the executor while we do.
executor.run(target.resumed()).await;
executor.run(target.resumed().wait()).await;

// Create a window.
let window = Window::new().await.unwrap();
let window = Window::<ThreadUnsafe>::new().await.unwrap();
state.borrow_mut().use_window(&window);

// Wait for the application to be suspended.
let mut suspend_guard = target.suspended().wait_guard();
let mut suspend_guard = target.suspended().wait();

// Wait for the window to close.
let mut wait_for_close = executor.spawn({
let window = window.clone();
async move {
window.close_requested().wait_once().await;
window.close_requested().await;
None
}
});
Expand All @@ -102,11 +103,11 @@ fn main2(event_loop: EventLoop) {

async move {
let mut graphics_context = None;
let mut draw_guard = window.redraw_requested().wait_guard();
let mut draw_guard = window.redraw_requested().wait();

loop {
// Wait until we need to draw.
let _guard = draw_guard.wait().await;
let _guard = draw_guard.hold().await;

// Get the window's size.
let size = window.inner_size().await;
Expand Down Expand Up @@ -145,7 +146,7 @@ fn main2(event_loop: EventLoop) {
async move {
window
.received_character()
.wait_many()
.wait()
.for_each(|ch| {
if (ch == 'R' || ch == 'r') && !state.borrow().running {
run_again.try_send(()).ok();
Expand All @@ -157,7 +158,7 @@ fn main2(event_loop: EventLoop) {

// Run the executor until either the window closes or the application suspends.
let hold_guard = async {
let hold_guard = suspend_guard.wait().await;
let hold_guard = suspend_guard.hold().await;
Some(hold_guard)
}
.or(executor.run(&mut wait_for_close))
Expand All @@ -168,8 +169,8 @@ fn main2(event_loop: EventLoop) {
rerun_http.cancel().await;
wait_for_close.cancel().await;
draw.cancel().await;
drop((window, guard));
state.borrow_mut().drop_window();
drop((window, guard));
} else {
target.exit().await;
}
Expand Down Expand Up @@ -386,7 +387,7 @@ enum HttpScheme {
struct State {
running: bool,
requests: Vec<HttpRequest>,
current_window: Option<Window>,
current_window: Option<Window<ThreadUnsafe>>,
}

impl State {
Expand All @@ -398,7 +399,7 @@ impl State {
}
}

fn use_window(&mut self, window: &Window) {
fn use_window(&mut self, window: &Window<ThreadUnsafe>) {
assert!(self.current_window.is_none());
self.current_window = Some(window.clone());
}
Expand Down
Loading

0 comments on commit 7f5ba27

Please sign in to comment.