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
Fix #1669
  • Loading branch information
cramertj committed Nov 5, 2019
1 parent 38b908f commit 78a9e55
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 79 deletions.
16 changes: 8 additions & 8 deletions futures-core/src/task/spawn.rs
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, ()>)
fn spawn_obj(&self, future: FutureObj<'static, ()>)
-> Result<(), SpawnError>;

/// Determines whether the executor is able to spawn new tasks.
Expand All @@ -38,7 +38,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, ()>)
fn spawn_local_obj(&self, future: LocalFutureObj<'static, ()>)
-> Result<(), SpawnError>;

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

impl<Sp: ?Sized + Spawn> Spawn for &mut Sp {
fn spawn_obj(&mut self, future: FutureObj<'static, ()>)
impl<Sp: ?Sized + Spawn> Spawn for &Sp {
fn spawn_obj(&self, future: FutureObj<'static, ()>)
-> Result<(), SpawnError> {
Sp::spawn_obj(self, future)
}
Expand All @@ -98,8 +98,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, ()>)
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 @@ -115,7 +115,7 @@ mod if_alloc {
use super::*;

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

impl<Sp: ?Sized + LocalSpawn> LocalSpawn for Box<Sp> {
fn spawn_local_obj(&mut self, future: LocalFutureObj<'static, ()>)
fn spawn_local_obj(&self, future: LocalFutureObj<'static, ()>)
-> Result<(), SpawnError> {
(**self).spawn_local_obj(future)
}
Expand Down
4 changes: 2 additions & 2 deletions futures-executor/src/local_pool.rs
Expand Up @@ -341,7 +341,7 @@ impl<S: Stream + Unpin> Iterator for BlockingStream<S> {

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

impl LocalSpawn for LocalSpawner {
fn spawn_local_obj(
&mut self,
&self,
future: LocalFutureObj<'static, ()>,
) -> Result<(), SpawnError> {
if let Some(incoming) = self.incoming.upgrade() {
Expand Down
12 changes: 1 addition & 11 deletions futures-executor/src/thread_pool.rs
Expand Up @@ -143,17 +143,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,
&self,
future: FutureObj<'static, ()>,
) -> Result<(), SpawnError> {
self.spawn_obj_ok(future);
Expand Down
24 changes: 12 additions & 12 deletions futures-executor/tests/local_pool.rs
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 |_| {
tx.send(()).unwrap();
()
Expand All @@ -69,8 +69,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(Box::pin(lazy(move |_| {
spawn2.spawn_local_obj(Box::pin(lazy(move |_| {
Expand All @@ -93,7 +93,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 @@ -121,7 +121,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 @@ -150,7 +150,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 @@ -189,7 +189,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 @@ -211,7 +211,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 All @@ -236,7 +236,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(Box::pin(lazy(|_| {
let mut pool = LocalPool::new();
Expand All @@ -250,7 +250,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(Box::pin(lazy(|_| {
let mut pool = LocalPool::new();
Expand Down Expand Up @@ -297,7 +297,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(),
Expand Down
2 changes: 1 addition & 1 deletion futures-test/src/task/noop_spawner.rs
Expand Up @@ -27,7 +27,7 @@ impl NoopSpawner {

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

impl Spawn for PanicSpawner {
fn spawn_obj(
&mut self,
&self,
_future: FutureObj<'static, ()>,
) -> Result<(), SpawnError> {
panic!("should not spawn")
Expand Down
23 changes: 8 additions & 15 deletions futures-test/src/task/record_spawner.rs
@@ -1,5 +1,6 @@
use futures_core::future::FutureObj;
use futures_core::task::{Spawn, SpawnError};
use std::cell::{Ref, RefCell};

/// An implementation of [`Spawn`](futures_core::task::Spawn) that records
/// any [`Future`](futures_core::future::Future)s spawned on it.
Expand All @@ -14,37 +15,29 @@ use futures_core::task::{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,
&self,
future: FutureObj<'static, ()>,
) -> Result<(), SpawnError> {
self.spawned.push(future);
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
Expand Up @@ -68,7 +68,7 @@ where
Ex: Executor01<Executor01Future> + Clone + Send + 'static,
{
fn spawn_obj(
&mut self,
&self,
future: FutureObj<'static, ()>,
) -> Result<(), SpawnError03> {
let future = future.unit_error().compat();
Expand Down

0 comments on commit 78a9e55

Please sign in to comment.