Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: bevy 0.14 #12

Merged
merged 3 commits into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
12 changes: 11 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,19 @@ Subheadings to categorize changes are `added, changed, deprecated, removed, fixe

## Unreleased

## 0.2.0

### Changed

- Updated to bevy 0.14

### Fixed

- Blocking example for web targets.

## 0.1.1

### fixed
### Fixed

- READMDE re-uploaded to correct [#10](https://github.com/loopystudios/bevy_async_task/issues/10).

Expand Down
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
24 changes: 17 additions & 7 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() {
AsyncTaskStatus::Idle => {
task_executor.start(long_task());
println!("Started new task!");
info!("Started new task!");
}
AsyncTaskStatus::Pending => {
// <Insert loading screen>
}
AsyncTaskStatus::Finished(v) => {
println!("Received {v}");
AsnycTaskStatus::Finished(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
25 changes: 8 additions & 17 deletions examples/blocking.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,22 @@
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;

/// You can block with a task runner
fn system1(mut task_executor: AsyncTaskRunner<u32>) {
let result = task_executor.blocking_recv(async {
sleep(Duration::from_millis(1000)).await;
1
});
println!("Received {result}");
let result = task_executor.blocking_recv(async { 1 });
info!("Received {result}");
}

/// Or block on a task, without the need of a system parameter.
fn system2() {
let result = AsyncTask::new(async {
sleep(Duration::from_millis(1000)).await;
2
})
.blocking_recv();
println!("Received {result}");
let result = AsyncTask::new(async { 2 }).blocking_recv();
info!("Received {result}");
}

pub fn main() {
App::new()
.add_plugins(MinimalPlugins)
.add_systems(Update, system1)
.add_systems(Update, system2)
.add_plugins((MinimalPlugins, LogPlugin::default(), PanicHandlerPlugin))
.add_systems(Startup, system2)
.add_systems(Startup, system1)
.run();
}
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();
}
8 changes: 4 additions & 4 deletions examples/timeout.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
use bevy::prelude::*;
use bevy::{app::PanicHandlerPlugin, log::LogPlugin, prelude::*};
use bevy_async_task::AsyncTask;
use std::time::Duration;

/// Use a timeout
fn system() {
let _timeout = AsyncTask::<()>::pending()
AsyncTask::<()>::pending()
.with_timeout(Duration::from_millis(1000))
.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
Loading