Skip to content

Commit

Permalink
feat: bevy 0.14
Browse files Browse the repository at this point in the history
  • Loading branch information
simbleau committed Jul 5, 2024
1 parent f814e32 commit 8c98fce
Show file tree
Hide file tree
Showing 12 changed files with 189 additions and 273 deletions.
5 changes: 5 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[alias]
run_wasm = "run --release --package run_wasm --"
# Other crates use the alias run-wasm, even though crate names should use `_`s not `-`s
# Allow this to be used
run-wasm = "run_wasm"
19 changes: 11 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,32 +1,35 @@
[workspace]
resolver = "2"
members = ["examples/run_wasm"]

[package]
name = "bevy_async_task"
description = "Ergonomic abstractions to async programming in Bevy"
license = "MIT/Apache-2.0"
license = "Apache-2.0 OR MIT"
repository = "https://github.com/loopystudios/bevy_async_task"
authors = ["Spencer C. Imbleau"]
keywords = ["gamedev", "async"]
version = "0.1.1"
version = "0.2.0"
edition = "2021"

[lib]

[dependencies]
tokio = { version = "1.36.0", default-features = false, features = ["sync"] }
bevy = { version = "0.13", default-features = false, features = [
"multi-threaded",
tokio = { version = "1.38.0", default-features = false, features = ["sync"] }
bevy = { version = "0.14.0", default-features = false, features = [
"multi_threaded",
] }
cfg-if = "1.0.0"
async-std = "1.12.0"

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
async-compat = "0.2.3"
async-compat = "0.2.4"

[dev-dependencies]
futures = "0.3.30"
futures-timer = "3.0.3"

[target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies]
tokio = { version = "1.36.0", features = ["full"] }
tokio = { version = "1.38.0", features = ["full"] }

[target.'cfg(target_arch = "wasm32")'.dev-dependencies]
wasm-bindgen-futures = "0.4.42"
Expand Down
22 changes: 16 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,22 @@ Bevy Async Task provides Bevy system parameters to run asyncronous tasks in the

|bevy|bevy_async_task|
|---|---|
|0.13|0.1, main|
|0.14|0.2, main|
|0.13|0.1|
|<= 0.13|Unsupported|

## Usage

Please see [examples](examples/) for more.
There are several [examples](examples/) for reference.

You can also run examples on web:

```shell
# Make sure the Rust toolchain supports the wasm32 target
rustup target add wasm32-unknown-unknown

cargo run_wasm --example simple
```

### Polling in systems

Expand All @@ -40,13 +50,13 @@ fn my_system(mut task_executor: AsyncTaskRunner<u32>) {
match task_executor.poll() {
AsnycTaskStatus::Idle => {
task_executor.start(long_task());
println!("Started new task!");
info!("Started new task!");
}
AsnycTaskStatus::Pending => {
// <Insert loading screen>
}
AsnycTaskStatus::Finished(v) => {
println!("Received {v}");
info!("Received {v}");
}
}
}
Expand All @@ -57,7 +67,7 @@ Poll many similar tasks simultaneously with `AsyncTaskPool<T>`:
```rust
fn my_system(mut task_pool: AsyncTaskPool<u64>) {
if task_pool.is_idle() {
println!("Queueing 5 tasks...");
info!("Queueing 5 tasks...");
for i in 1..=5 {
task_pool.spawn(async move { // Closures work too!
sleep(Duration::from_millis(i * 1000)).await;
Expand All @@ -68,7 +78,7 @@ fn my_system(mut task_pool: AsyncTaskPool<u64>) {

for status in task_pool.iter_poll() {
if let AsyncTaskStatus::Finished(t) = status {
println!("Received {t}");
info!("Received {t}");
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions examples/blocking.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use async_std::task::sleep;
use bevy::prelude::*;
use bevy::{app::PanicHandlerPlugin, log::LogPlugin, prelude::*};
use bevy_async_task::{AsyncTask, AsyncTaskRunner};
use std::time::Duration;

Expand All @@ -9,7 +9,7 @@ fn system1(mut task_executor: AsyncTaskRunner<u32>) {
sleep(Duration::from_millis(1000)).await;
1
});
println!("Received {result}");
info!("Received {result}");
}

/// Or block on a task, without the need of a system parameter.
Expand All @@ -19,12 +19,12 @@ fn system2() {
2
})
.blocking_recv();
println!("Received {result}");
info!("Received {result}");
}

pub fn main() {
App::new()
.add_plugins(MinimalPlugins)
.add_plugins((MinimalPlugins, LogPlugin::default(), PanicHandlerPlugin))
.add_systems(Update, system1)
.add_systems(Update, system2)
.run();
Expand Down
8 changes: 4 additions & 4 deletions examples/pool.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use async_std::task::sleep;
use bevy::prelude::*;
use bevy::{app::PanicHandlerPlugin, log::LogPlugin, prelude::*};
use bevy_async_task::{AsyncTaskPool, AsyncTaskStatus};
use std::time::Duration;

fn system1(mut task_pool: AsyncTaskPool<u64>) {
if task_pool.is_idle() {
println!("Queueing 5 tasks...");
info!("Queueing 5 tasks...");
for i in 1..=5 {
task_pool.spawn(async move {
sleep(Duration::from_millis(i * 1000)).await;
Expand All @@ -16,14 +16,14 @@ fn system1(mut task_pool: AsyncTaskPool<u64>) {

for status in task_pool.iter_poll() {
if let AsyncTaskStatus::Finished(t) = status {
println!("Received {t}");
info!("Received {t}");
}
}
}

pub fn main() {
App::new()
.add_plugins(MinimalPlugins)
.add_plugins((MinimalPlugins, LogPlugin::default(), PanicHandlerPlugin))
.add_systems(Update, system1)
.run();
}
6 changes: 6 additions & 0 deletions examples/run_wasm/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "run_wasm"
publish = false

[dependencies]
cargo-run-wasm = "0.4.0"
14 changes: 14 additions & 0 deletions examples/run_wasm/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/// Use [cargo-run-wasm](https://github.com/rukai/cargo-run-wasm) to build an example for web
///
/// Usage:
/// ```
/// cargo run_wasm --example [example_name]
/// ```
/// Generally:
/// ```
/// cargo run_wasm --example blocking
/// ```
fn main() {
cargo_run_wasm::run_wasm_cli_with_css("body { margin: 0px; }");
}
8 changes: 4 additions & 4 deletions examples/simple.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use async_std::task::sleep;
use bevy::prelude::*;
use bevy::{app::PanicHandlerPlugin, log::LogPlugin, prelude::*};
use bevy_async_task::{AsyncTaskRunner, AsyncTaskStatus};
use std::time::Duration;

Expand All @@ -16,20 +16,20 @@ fn my_system(mut task_executor: AsyncTaskRunner<u32>) {
task_executor.start(long_task());
// Closures also work:
// task_executor.start(async { 5 });
println!("Started!");
info!("Started!");
}
AsyncTaskStatus::Pending => {
// Waiting...
}
AsyncTaskStatus::Finished(v) => {
println!("Received {v}");
info!("Received {v}");
}
}
}

pub fn main() {
App::new()
.add_plugins(MinimalPlugins)
.add_plugins((MinimalPlugins, LogPlugin::default(), PanicHandlerPlugin))
.add_systems(Update, my_system)
.run();
}
6 changes: 3 additions & 3 deletions examples/timeout.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use bevy::prelude::*;
use bevy::{app::PanicHandlerPlugin, log::LogPlugin, prelude::*};
use bevy_async_task::AsyncTask;
use std::time::Duration;

Expand All @@ -9,12 +9,12 @@ fn system() {
.blocking_recv()
.unwrap_err();

println!("Timeout!");
info!("Timeout!");
}

pub fn main() {
App::new()
.add_plugins(MinimalPlugins)
.add_plugins((MinimalPlugins, LogPlugin::default(), PanicHandlerPlugin))
.add_systems(Update, system)
.run();
}
17 changes: 2 additions & 15 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,16 @@
#![deny(missing_docs)]
//! Ergonomic abstractions to async programming in Bevy for all platforms.
use cfg_if::cfg_if;

mod receiver;
mod task;
mod task_pool;
mod task_runner;

// Re-exports
pub use async_std::future::TimeoutError;

pub use receiver::AsyncReceiver;
pub use task::{AsyncTask, TimeoutError};
pub use task_pool::AsyncTaskPool;
pub use task_runner::AsyncTaskRunner;

cfg_if! {
if #[cfg(target_arch = "wasm32")] {
mod wasm;
pub use wasm::AsyncTask;
} else {
mod native;
pub use native::AsyncTask;
}
}

/// A poll status for an [`AsyncTask`].
pub enum AsyncTaskStatus<T> {
/// No task is currently being polled.
Expand Down
Loading

0 comments on commit 8c98fce

Please sign in to comment.