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

Make Spawn::spawn take &self rather than &mut self #1950

Merged
merged 1 commit into from Nov 5, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions futures-executor/src/local_pool.rs
Expand Up @@ -333,7 +333,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 @@ -355,7 +355,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 @@ -131,17 +131,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
32 changes: 16 additions & 16 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 @@ -182,10 +182,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(Box::pin(poll_fn(move |_| {
cnt1.set(cnt1.get() + 1);
Expand All @@ -212,7 +212,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 @@ -229,10 +229,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(Box::pin(poll_fn(move |_| {
cnt1.set(cnt1.get() + 1);
Expand All @@ -257,7 +257,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 @@ -282,7 +282,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 @@ -296,7 +296,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 @@ -343,7 +343,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
14 changes: 7 additions & 7 deletions futures-task/src/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, ()>) -> 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,7 +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
Expand Up @@ -26,7 +26,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 @@ -27,7 +27,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,4 +1,5 @@
use futures_task::{Spawn, SpawnError, FutureObj};
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,37 +14,29 @@ use futures_task::{Spawn, SpawnError, FutureObj};
/// 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 @@ -67,7 +67,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