Skip to content

Commit

Permalink
refactor: fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
simbleau committed Mar 24, 2024
1 parent 20993ea commit a8a91fb
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 53 deletions.
13 changes: 3 additions & 10 deletions src/native/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@ where
}

/// Add a timeout to the task.
pub fn with_timeout(
mut self,
dur: Duration,
) -> AsyncTask<Result<T, TimeoutError>> {
pub fn with_timeout(mut self, dur: Duration) -> AsyncTask<Result<T, TimeoutError>> {
let (tx, rx) = oneshot::channel();
let new_fut = async move {
let result = timeout(dur, self.fut)
Expand Down Expand Up @@ -64,10 +61,7 @@ impl<T> AsyncTask<T> {
}

/// Create an async task from a future with a timeout.
pub fn new_with_timeout<F>(
dur: Duration,
fut: F,
) -> AsyncTask<Result<T, TimeoutError>>
pub fn new_with_timeout<F>(dur: Duration, fut: F) -> AsyncTask<Result<T, TimeoutError>>
where
F: Future<Output = T> + Send + 'static,
F::Output: Send + 'static,
Expand Down Expand Up @@ -213,8 +207,7 @@ mod test {

#[tokio::test]
async fn test_with_timeout() {
let task =
AsyncTask::<()>::pending().with_timeout(Duration::from_millis(5));
let task = AsyncTask::<()>::pending().with_timeout(Duration::from_millis(5));
let (fut, mut rx) = task.into_parts();

assert_eq!(None, rx.try_recv());
Expand Down
23 changes: 5 additions & 18 deletions src/task_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ use crate::{AsyncReceiver, AsyncTask, AsyncTaskStatus};
use bevy::{
ecs::{
component::Tick,
system::{
ExclusiveSystemParam, ReadOnlySystemParam, SystemMeta, SystemParam,
},
system::{ExclusiveSystemParam, ReadOnlySystemParam, SystemMeta, SystemParam},
world::unsafe_world_cell::UnsafeWorldCell,
},
prelude::*,
Expand All @@ -14,9 +12,7 @@ use bevy::{

/// 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>>>,
);
pub struct AsyncTaskPool<'s, T>(pub(crate) &'s mut Vec<Option<AsyncReceiver<T>>>);

impl<'s, T> AsyncTaskPool<'s, T> {
/// Returns whether the task pool is idle.
Expand Down Expand Up @@ -66,29 +62,20 @@ impl<'_s, T: Send + 'static> ExclusiveSystemParam for AsyncTaskPool<'_s, T> {
}

#[inline]
fn get_param<'s>(
state: &'s mut Self::State,
_system_meta: &SystemMeta,
) -> Self::Item<'s> {
fn get_param<'s>(state: &'s mut Self::State, _system_meta: &SystemMeta) -> Self::Item<'s> {
AsyncTaskPool(state.get())
}
}

// SAFETY: only local state is accessed
unsafe impl<'s, T: Send + 'static> ReadOnlySystemParam
for AsyncTaskPool<'s, T>
{
}
unsafe impl<'s, T: Send + 'static> ReadOnlySystemParam for AsyncTaskPool<'s, T> {}

// SAFETY: only local state is accessed
unsafe impl<'a, T: Send + 'static> SystemParam for AsyncTaskPool<'a, T> {
type State = SyncCell<Vec<Option<AsyncReceiver<T>>>>;
type Item<'w, 's> = AsyncTaskPool<'s, T>;

fn init_state(
_world: &mut World,
_system_meta: &mut SystemMeta,
) -> Self::State {
fn init_state(_world: &mut World, _system_meta: &mut SystemMeta) -> Self::State {
SyncCell::new(vec![])
}

Expand Down
19 changes: 4 additions & 15 deletions src/task_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ use crate::{AsyncReceiver, AsyncTask, AsyncTaskStatus};
use bevy::{
ecs::{
component::Tick,
system::{
ExclusiveSystemParam, ReadOnlySystemParam, SystemMeta, SystemParam,
},
system::{ExclusiveSystemParam, ReadOnlySystemParam, SystemMeta, SystemParam},
world::unsafe_world_cell::UnsafeWorldCell,
},
prelude::*,
Expand Down Expand Up @@ -88,29 +86,20 @@ impl<'_s, T: Send + 'static> ExclusiveSystemParam for AsyncTaskRunner<'_s, T> {
SyncCell::new(None)
}

fn get_param<'s>(
state: &'s mut Self::State,
_system_meta: &SystemMeta,
) -> Self::Item<'s> {
fn get_param<'s>(state: &'s mut Self::State, _system_meta: &SystemMeta) -> Self::Item<'s> {
AsyncTaskRunner(state.get())
}
}

// SAFETY: only local state is accessed
unsafe impl<'s, T: Send + 'static> ReadOnlySystemParam
for AsyncTaskRunner<'s, T>
{
}
unsafe impl<'s, T: Send + 'static> ReadOnlySystemParam for AsyncTaskRunner<'s, T> {}

// SAFETY: only local state is accessed
unsafe impl<'a, T: Send + 'static> SystemParam for AsyncTaskRunner<'a, T> {
type State = SyncCell<Option<AsyncReceiver<T>>>;
type Item<'w, 's> = AsyncTaskRunner<'s, T>;

fn init_state(
_world: &mut World,
_system_meta: &mut SystemMeta,
) -> Self::State {
fn init_state(_world: &mut World, _system_meta: &mut SystemMeta) -> Self::State {
SyncCell::new(None)
}

Expand Down
13 changes: 3 additions & 10 deletions src/wasm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@ where
T: 'static,
{
/// Add a timeout to the task.
pub fn with_timeout(
mut self,
dur: Duration,
) -> AsyncTask<Result<T, TimeoutError>> {
pub fn with_timeout(mut self, dur: Duration) -> AsyncTask<Result<T, TimeoutError>> {
let (tx, rx) = oneshot::channel();
let new_fut = async move {
let result = timeout(dur, self.fut)
Expand Down Expand Up @@ -68,10 +65,7 @@ impl<T> AsyncTask<T> {
}

/// Create an async task from a future with a timeout.
pub fn new_with_timeout<F>(
dur: Duration,
fut: F,
) -> AsyncTask<Result<T, TimeoutError>>
pub fn new_with_timeout<F>(dur: Duration, fut: F) -> AsyncTask<Result<T, TimeoutError>>
where
F: Future<Output = T> + 'static,
F::Output: Send + 'static,
Expand Down Expand Up @@ -199,8 +193,7 @@ mod test {

#[wasm_bindgen_test]
async fn test_with_timeout() {
let task =
AsyncTask::<()>::pending().with_timeout(Duration::from_millis(5));
let task = AsyncTask::<()>::pending().with_timeout(Duration::from_millis(5));
let (fut, mut rx) = task.into_parts();

assert_eq!(None, rx.try_recv());
Expand Down

0 comments on commit a8a91fb

Please sign in to comment.