Skip to content

Commit

Permalink
Make Spawn::spawn take &self rather than &mut self
Browse files Browse the repository at this point in the history
  • Loading branch information
cramertj committed Nov 5, 2019
1 parent 920caff commit 50d93aa
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 80 deletions.
4 changes: 2 additions & 2 deletions futures-executor/src/local_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ impl<S: Stream + Unpin> Iterator for BlockingStream<S> {
}

impl Spawn for LocalSpawner {
fn spawn_obj(&mut self, future: FutureObj<'static, ()>) -> Result<(), SpawnError> {
fn spawn_obj(&self, future: FutureObj<'static, ()>) -> Result<(), SpawnError> {
if let Some(incoming) = self.incoming.upgrade() {
incoming.borrow_mut().push(future.into());
Ok(())
Expand All @@ -350,7 +350,7 @@ impl Spawn for LocalSpawner {
}

impl LocalSpawn for LocalSpawner {
fn spawn_local_obj(&mut self, future: LocalFutureObj<'static, ()>) -> Result<(), SpawnError> {
fn spawn_local_obj(&self, future: LocalFutureObj<'static, ()>) -> Result<(), SpawnError> {
if let Some(incoming) = self.incoming.upgrade() {
incoming.borrow_mut().push(future);
Ok(())
Expand Down
9 changes: 1 addition & 8 deletions futures-executor/src/thread_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,7 @@ impl ThreadPool {
}

impl Spawn for ThreadPool {
fn spawn_obj(&mut self, future: FutureObj<'static, ()>) -> Result<(), SpawnError> {
self.spawn_obj_ok(future);
Ok(())
}
}

impl Spawn for &ThreadPool {
fn spawn_obj(&mut self, future: FutureObj<'static, ()>) -> Result<(), SpawnError> {
fn spawn_obj(&self, future: FutureObj<'static, ()>) -> Result<(), SpawnError> {
self.spawn_obj_ok(future);
Ok(())
}
Expand Down
32 changes: 16 additions & 16 deletions futures-executor/tests/local_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ fn run_until_single_future() {
#[test]
fn run_until_ignores_spawned() {
let mut pool = LocalPool::new();
let mut spawn = pool.spawner();
let spawn = pool.spawner();
spawn.spawn_local_obj(Box::pin(pending()).into()).unwrap();
assert_eq!(pool.run_until(lazy(|_| ())), ());
}
Expand All @@ -48,7 +48,7 @@ fn run_until_ignores_spawned() {
fn run_until_executes_spawned() {
let (tx, rx) = oneshot::channel();
let mut pool = LocalPool::new();
let mut spawn = pool.spawner();
let spawn = pool.spawner();
spawn
.spawn_local_obj(
Box::pin(lazy(move |_| {
Expand All @@ -74,8 +74,8 @@ fn run_executes_spawned() {
let cnt2 = cnt.clone();

let mut pool = LocalPool::new();
let mut spawn = pool.spawner();
let mut spawn2 = pool.spawner();
let spawn = pool.spawner();
let spawn2 = pool.spawner();

spawn
.spawn_local_obj(
Expand Down Expand Up @@ -107,7 +107,7 @@ fn run_spawn_many() {
let cnt = Rc::new(Cell::new(0));

let mut pool = LocalPool::new();
let mut spawn = pool.spawner();
let spawn = pool.spawner();

for _ in 0..ITER {
let cnt = cnt.clone();
Expand Down Expand Up @@ -140,7 +140,7 @@ fn try_run_one_executes_one_ready() {
let cnt = Rc::new(Cell::new(0));

let mut pool = LocalPool::new();
let mut spawn = pool.spawner();
let spawn = pool.spawner();

for _ in 0..ITER {
spawn.spawn_local_obj(Box::pin(pending()).into()).unwrap();
Expand Down Expand Up @@ -174,7 +174,7 @@ fn try_run_one_returns_on_no_progress() {
let cnt = Rc::new(Cell::new(0));

let mut pool = LocalPool::new();
let mut spawn = pool.spawner();
let spawn = pool.spawner();

let waker: Rc<Cell<Option<Waker>>> = Rc::new(Cell::new(None));
{
Expand Down Expand Up @@ -211,10 +211,10 @@ fn try_run_one_returns_on_no_progress() {
#[test]
fn try_run_one_runs_sub_futures() {
let mut pool = LocalPool::new();
let mut spawn = pool.spawner();
let spawn = pool.spawner();
let cnt = Rc::new(Cell::new(0));

let mut inner_spawner = spawn.clone();
let inner_spawner = spawn.clone();
let cnt1 = cnt.clone();
spawn
.spawn_local_obj(
Expand Down Expand Up @@ -246,7 +246,7 @@ fn run_until_stalled_returns_if_empty() {
#[test]
fn run_until_stalled_returns_multiple_times() {
let mut pool = LocalPool::new();
let mut spawn = pool.spawner();
let spawn = pool.spawner();
let cnt = Rc::new(Cell::new(0));

let cnt1 = cnt.clone();
Expand All @@ -263,10 +263,10 @@ fn run_until_stalled_returns_multiple_times() {
#[test]
fn run_until_stalled_runs_spawned_sub_futures() {
let mut pool = LocalPool::new();
let mut spawn = pool.spawner();
let spawn = pool.spawner();
let cnt = Rc::new(Cell::new(0));

let mut inner_spawner = spawn.clone();
let inner_spawner = spawn.clone();
let cnt1 = cnt.clone();
spawn
.spawn_local_obj(
Expand Down Expand Up @@ -296,7 +296,7 @@ fn run_until_stalled_executes_all_ready() {
let cnt = Rc::new(Cell::new(0));

let mut pool = LocalPool::new();
let mut spawn = pool.spawner();
let spawn = pool.spawner();

for i in 0..ITER {
for _ in 0..PER_ITER {
Expand Down Expand Up @@ -326,7 +326,7 @@ fn run_until_stalled_executes_all_ready() {
#[should_panic]
fn nesting_run() {
let mut pool = LocalPool::new();
let mut spawn = pool.spawner();
let spawn = pool.spawner();

spawn
.spawn_obj(
Expand All @@ -345,7 +345,7 @@ fn nesting_run() {
#[should_panic]
fn nesting_run_run_until_stalled() {
let mut pool = LocalPool::new();
let mut spawn = pool.spawner();
let spawn = pool.spawner();

spawn
.spawn_obj(
Expand Down Expand Up @@ -397,7 +397,7 @@ fn tasks_are_scheduled_fairly() {
}

let mut pool = LocalPool::new();
let mut spawn = pool.spawner();
let spawn = pool.spawner();

spawn.spawn_local_obj(Box::pin(Spin { state: state.clone(), idx: 0 }).into()).unwrap();

Expand Down
17 changes: 7 additions & 10 deletions futures-task/src/spawn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub trait Spawn {
/// represent relatively rare scenarios, such as the executor
/// having been shut down so that it is no longer able to accept
/// tasks.
fn spawn_obj(&mut self, future: FutureObj<'static, ()>) -> Result<(), SpawnError>;
fn spawn_obj(&self, future: FutureObj<'static, ()>) -> Result<(), SpawnError>;

/// Determines whether the executor is able to spawn new tasks.
///
Expand All @@ -37,7 +37,7 @@ pub trait LocalSpawn {
/// represent relatively rare scenarios, such as the executor
/// having been shut down so that it is no longer able to accept
/// tasks.
fn spawn_local_obj(&mut self, future: LocalFutureObj<'static, ()>) -> Result<(), SpawnError>;
fn spawn_local_obj(&self, future: LocalFutureObj<'static, ()>) -> Result<(), SpawnError>;

/// Determines whether the executor is able to spawn new tasks.
///
Expand Down Expand Up @@ -84,7 +84,7 @@ impl SpawnError {
}

impl<Sp: ?Sized + Spawn> Spawn for &mut Sp {
fn spawn_obj(&mut self, future: FutureObj<'static, ()>) -> Result<(), SpawnError> {
fn spawn_obj(&self, future: FutureObj<'static, ()>) -> Result<(), SpawnError> {
Sp::spawn_obj(self, future)
}

Expand All @@ -93,8 +93,8 @@ impl<Sp: ?Sized + Spawn> Spawn for &mut Sp {
}
}

impl<Sp: ?Sized + LocalSpawn> LocalSpawn for &mut Sp {
fn spawn_local_obj(&mut self, future: LocalFutureObj<'static, ()>) -> Result<(), SpawnError> {
impl<Sp: ?Sized + LocalSpawn> LocalSpawn for &Sp {
fn spawn_local_obj(&self, future: LocalFutureObj<'static, ()>) -> Result<(), SpawnError> {
Sp::spawn_local_obj(self, future)
}

Expand All @@ -109,7 +109,7 @@ mod if_alloc {
use alloc::boxed::Box;

impl<Sp: ?Sized + Spawn> Spawn for Box<Sp> {
fn spawn_obj(&mut self, future: FutureObj<'static, ()>) -> Result<(), SpawnError> {
fn spawn_obj(&self, future: FutureObj<'static, ()>) -> Result<(), SpawnError> {
(**self).spawn_obj(future)
}

Expand All @@ -119,10 +119,7 @@ mod if_alloc {
}

impl<Sp: ?Sized + LocalSpawn> LocalSpawn for Box<Sp> {
fn spawn_local_obj(
&mut self,
future: LocalFutureObj<'static, ()>,
) -> Result<(), SpawnError> {
fn spawn_local_obj(&self, future: LocalFutureObj<'static, ()>) -> Result<(), SpawnError> {
(**self).spawn_local_obj(future)
}

Expand Down
2 changes: 1 addition & 1 deletion futures-test/src/task/noop_spawner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl NoopSpawner {
}

impl Spawn for NoopSpawner {
fn spawn_obj(&mut self, _future: FutureObj<'static, ()>) -> Result<(), SpawnError> {
fn spawn_obj(&self, _future: FutureObj<'static, ()>) -> Result<(), SpawnError> {
Ok(())
}
}
Expand Down
2 changes: 1 addition & 1 deletion futures-test/src/task/panic_spawner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl PanicSpawner {
}

impl Spawn for PanicSpawner {
fn spawn_obj(&mut self, _future: FutureObj<'static, ()>) -> Result<(), SpawnError> {
fn spawn_obj(&self, _future: FutureObj<'static, ()>) -> Result<(), SpawnError> {
panic!("should not spawn")
}
}
Expand Down
21 changes: 8 additions & 13 deletions futures-test/src/task/record_spawner.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use futures_task::{FutureObj, Spawn, SpawnError};
use std::cell::{Ref, RefCell};

/// An implementation of [`Spawn`](futures_task::Spawn) that records
/// any [`Future`](futures_core::future::Future)s spawned on it.
Expand All @@ -13,32 +14,26 @@ use futures_task::{FutureObj, Spawn, SpawnError};
/// recorder.spawn(async { }).unwrap();
/// assert_eq!(recorder.spawned().len(), 1);
/// ```
#[derive(Debug)]
#[derive(Debug, Default)]
pub struct RecordSpawner {
spawned: Vec<FutureObj<'static, ()>>,
spawned: RefCell<Vec<FutureObj<'static, ()>>>,
}

impl RecordSpawner {
/// Create a new instance
pub fn new() -> Self {
Self { spawned: Vec::new() }
Default::default()
}

/// Inspect any futures that were spawned onto this [`Spawn`].
pub fn spawned(&self) -> &[FutureObj<'static, ()>] {
&self.spawned
pub fn spawned(&self) -> Ref<'_, Vec<FutureObj<'static, ()>>> {
self.spawned.borrow()
}
}

impl Spawn for RecordSpawner {
fn spawn_obj(&mut self, future: FutureObj<'static, ()>) -> Result<(), SpawnError> {
self.spawned.push(future);
fn spawn_obj(&self, future: FutureObj<'static, ()>) -> Result<(), SpawnError> {
self.spawned.borrow_mut().push(future);
Ok(())
}
}

impl Default for RecordSpawner {
fn default() -> Self {
Self::new()
}
}
2 changes: 1 addition & 1 deletion futures-util/src/compat/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ impl<Ex> Spawn03 for Executor01As03<Ex>
where
Ex: Executor01<Executor01Future> + Clone + Send + 'static,
{
fn spawn_obj(&mut self, future: FutureObj<'static, ()>) -> Result<(), SpawnError03> {
fn spawn_obj(&self, future: FutureObj<'static, ()>) -> Result<(), SpawnError03> {
let future = future.unit_error().compat();

self.executor01.execute(future).map_err(|_| SpawnError03::shutdown())
Expand Down
Loading

0 comments on commit 50d93aa

Please sign in to comment.