Skip to content

Commit

Permalink
docs: fix all links and enforce docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Spencer C. Imbleau committed Sep 9, 2023
1 parent 1a3475a commit 2ccfe4f
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 15 deletions.
5 changes: 4 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#![deny(missing_docs)]
//! Ergonomic abstractions to async programming in Bevy for all platforms.

use cfg_if::cfg_if;

mod receiver;
Expand All @@ -18,7 +21,7 @@ cfg_if! {
}
}

/// The status of an [`AsyncTask`].
/// A poll status for an [`AsyncTask`].
pub enum AsyncTaskStatus<T> {
/// No task is currently being polled.
Idle,
Expand Down
8 changes: 5 additions & 3 deletions src/native/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ use futures::channel::oneshot;
use std::{future::Future, pin::Pin};

/// A wrapper type around an async future. The future may be executed
/// asynchronously by an [`AsyncTaskRunner`] or [`AsyncTaskPool`], or it may be
/// blocked on the current thread.
/// asynchronously by an [`AsyncTaskRunner`](crate::AsyncTaskRunner) or
/// [`AsyncTaskPool`](crate::AsyncTaskPool), or it may be blocked on the current
/// thread.
pub struct AsyncTask<T> {
fut: Pin<Box<dyn Future<Output = ()> + Send + 'static>>,
receiver: AsyncReceiver<T>,
}

impl<T> AsyncTask<T> {
/// Create an async task from a future.
pub fn new<F>(fut: F) -> Self
where
F: Future<Output = T> + Send + 'static,
Expand Down Expand Up @@ -85,7 +87,7 @@ mod test {

match rx.await {
Ok(v) => assert_eq!(3, v),
Err(_) => panic!("the sender dropped"),
Err(e) => panic!("the sender dropped ({e})"),
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/receiver.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/// A channel that catches an [`AsyncTask`](crate::AsyncTask) result.
pub struct AsyncReceiver<T> {
pub(crate) received: bool,
pub(crate) buffer: futures::channel::oneshot::Receiver<T>,
Expand All @@ -16,7 +17,7 @@ impl<T> AsyncReceiver<T> {
Some(t)
}
Ok(None) => None,
Err(_) => panic!("the sender was dropped without sending"),
Err(e) => panic!("the sender was dropped without sending ({e})"),
}
}
}
12 changes: 6 additions & 6 deletions src/task_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ use bevy::{
utils::synccell::SyncCell,
};

/// A task pool which executes [`AsyncTask`]s in the background.
/// A Bevy [`SystemParam`] to execute many similar [`AsyncTask`]s in the
/// background simultaneously.
pub struct AsyncTaskPool<'s, T>(
pub(crate) &'s mut Vec<Option<AsyncReceiver<T>>>,
);
Expand All @@ -36,16 +37,15 @@ impl<'s, T> AsyncTaskPool<'s, T> {
pub fn iter_poll(&mut self) -> impl Iterator<Item = AsyncTaskStatus<T>> {
let mut statuses = vec![];
self.0.retain_mut(|task| match task {
Some(rx) => match rx.try_recv() {
Some(v) => {
Some(rx) => {
if let Some(v) = rx.try_recv() {
statuses.push(AsyncTaskStatus::Finished(v));
false
}
None => {
} else {
statuses.push(AsyncTaskStatus::Pending);
true
}
},
}
None => {
statuses.push(AsyncTaskStatus::Idle);
true
Expand Down
5 changes: 3 additions & 2 deletions src/task_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ use bevy::{
utils::synccell::SyncCell,
};

/// A task runner which can execute a single [`AsyncTask`] in the background.
/// A Bevy [`SystemParam`] to execute [`AsyncTask`]s individually in the
/// background.
pub struct AsyncTaskRunner<'s, T>(pub(crate) &'s mut Option<AsyncReceiver<T>>);

impl<'s, T> AsyncTaskRunner<'s, T> {
Expand Down Expand Up @@ -49,7 +50,7 @@ impl<'s, T> AsyncTaskRunner<'s, T> {

/// Start an async task in the background. If there is an existing task
/// pending, it will be dropped and replaced with the given task. If you
/// need to run multiple tasks, use the [AsyncTaskPool].
/// need to run multiple tasks, use the [`AsyncTaskPool`].
pub fn start(&mut self, task: impl Into<AsyncTask<T>>) {
let task = task.into();
let (fut, rx) = task.into_parts();
Expand Down
8 changes: 6 additions & 2 deletions src/wasm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@ use crate::AsyncReceiver;
use futures::channel::oneshot;
use std::{future::Future, pin::Pin};

/// A task than may be ran by an [`AsyncTaskRunner`], or broken into parts.
/// A wrapper type around an async future. The future may be executed
/// asynchronously by an [`AsyncTaskRunner`](crate::AsyncTaskRunner) or
/// [`AsyncTaskPool`](crate::AsyncTaskPool), or it may be blocked on the current
/// thread.
pub struct AsyncTask<T> {
fut: Pin<Box<dyn Future<Output = ()> + 'static>>,
receiver: AsyncReceiver<T>,
}

impl<T> AsyncTask<T> {
/// Create an async task from a future.
pub fn new<F>(fut: F) -> Self
where
F: Future<Output = T> + 'static,
Expand Down Expand Up @@ -79,7 +83,7 @@ mod test {

match rx.await {
Ok(v) => assert_eq!(3, v),
Err(_) => panic!("the sender dropped"),
Err(e) => panic!("the sender dropped ({e})"),
}
});
}
Expand Down

0 comments on commit 2ccfe4f

Please sign in to comment.