From 8aec341f28a00012e1771d5c35d2647e11830755 Mon Sep 17 00:00:00 2001 From: Dario Nieuwenhuis Date: Wed, 9 Jul 2025 01:49:31 +0200 Subject: executor: return error when creating the spawntoken, not when spawning. --- embassy-executor/tests/test.rs | 29 +++++++++++----------- .../tests/ui/return_impl_future_nonsend.rs | 2 +- .../tests/ui/return_impl_future_nonsend.stderr | 6 ++--- embassy-executor/tests/ui/return_impl_send.stderr | 2 +- embassy-executor/tests/ui/spawn_nonsend.rs | 2 +- embassy-executor/tests/ui/spawn_nonsend.stderr | 10 ++++---- 6 files changed, 25 insertions(+), 26 deletions(-) (limited to 'embassy-executor/tests') diff --git a/embassy-executor/tests/test.rs b/embassy-executor/tests/test.rs index 530314ac3..85c5dc1d9 100644 --- a/embassy-executor/tests/test.rs +++ b/embassy-executor/tests/test.rs @@ -65,7 +65,7 @@ fn executor_task() { } let (executor, trace) = setup(); - executor.spawner().spawn(task1(trace.clone())).unwrap(); + executor.spawner().spawn(task1(trace.clone()).unwrap()); unsafe { executor.poll() }; unsafe { executor.poll() }; @@ -93,7 +93,7 @@ fn executor_task_rpit() { } let (executor, trace) = setup(); - executor.spawner().spawn(task1(trace.clone())).unwrap(); + executor.spawner().spawn(task1(trace.clone()).unwrap()); unsafe { executor.poll() }; unsafe { executor.poll() }; @@ -120,7 +120,7 @@ fn executor_task_self_wake() { } let (executor, trace) = setup(); - executor.spawner().spawn(task1(trace.clone())).unwrap(); + executor.spawner().spawn(task1(trace.clone()).unwrap()); unsafe { executor.poll() }; unsafe { executor.poll() }; @@ -152,7 +152,7 @@ fn executor_task_self_wake_twice() { } let (executor, trace) = setup(); - executor.spawner().spawn(task1(trace.clone())).unwrap(); + executor.spawner().spawn(task1(trace.clone()).unwrap()); unsafe { executor.poll() }; unsafe { executor.poll() }; @@ -188,7 +188,7 @@ fn waking_after_completion_does_not_poll() { let waker = Box::leak(Box::new(AtomicWaker::new())); let (executor, trace) = setup(); - executor.spawner().spawn(task1(trace.clone(), waker)).unwrap(); + executor.spawner().spawn(task1(trace.clone(), waker).unwrap()); unsafe { executor.poll() }; waker.wake(); @@ -200,7 +200,7 @@ fn waking_after_completion_does_not_poll() { unsafe { executor.poll() }; // Clears running status // Can respawn waken-but-dead task - executor.spawner().spawn(task1(trace.clone(), waker)).unwrap(); + executor.spawner().spawn(task1(trace.clone(), waker).unwrap()); unsafe { executor.poll() }; @@ -250,7 +250,7 @@ fn waking_with_old_waker_after_respawn() { let waker = Box::leak(Box::new(AtomicWaker::new())); let (executor, trace) = setup(); - executor.spawner().spawn(task1(trace.clone(), waker)).unwrap(); + executor.spawner().spawn(task1(trace.clone(), waker).unwrap()); unsafe { executor.poll() }; unsafe { executor.poll() }; // progress to registering the waker @@ -273,8 +273,7 @@ fn waking_with_old_waker_after_respawn() { let (other_executor, other_trace) = setup(); other_executor .spawner() - .spawn(task1(other_trace.clone(), waker)) - .unwrap(); + .spawn(task1(other_trace.clone(), waker).unwrap()); unsafe { other_executor.poll() }; // just run to the yield_now waker.wake(); // trigger old waker registration @@ -338,22 +337,22 @@ fn task_metadata() { // check no task name let (executor, _) = setup(); - executor.spawner().spawn(task1(None)).unwrap(); + executor.spawner().spawn(task1(None).unwrap()); unsafe { executor.poll() }; // check setting task name - let token = task1(Some("foo")); + let token = task1(Some("foo")).unwrap(); token.metadata().set_name("foo"); - executor.spawner().spawn(token).unwrap(); + executor.spawner().spawn(token); unsafe { executor.poll() }; - let token = task1(Some("bar")); + let token = task1(Some("bar")).unwrap(); token.metadata().set_name("bar"); - executor.spawner().spawn(token).unwrap(); + executor.spawner().spawn(token); unsafe { executor.poll() }; // check name is cleared if the task pool slot is recycled. let (executor, _) = setup(); - executor.spawner().spawn(task1(None)).unwrap(); + executor.spawner().spawn(task1(None).unwrap()); unsafe { executor.poll() }; } diff --git a/embassy-executor/tests/ui/return_impl_future_nonsend.rs b/embassy-executor/tests/ui/return_impl_future_nonsend.rs index b8c184b21..77b3119d6 100644 --- a/embassy-executor/tests/ui/return_impl_future_nonsend.rs +++ b/embassy-executor/tests/ui/return_impl_future_nonsend.rs @@ -15,7 +15,7 @@ fn task() -> impl Future { } fn send_spawn(s: SendSpawner) { - s.spawn(task()).unwrap(); + s.spawn(task().unwrap()); } fn main() {} diff --git a/embassy-executor/tests/ui/return_impl_future_nonsend.stderr b/embassy-executor/tests/ui/return_impl_future_nonsend.stderr index 8aeb9738a..51944ad65 100644 --- a/embassy-executor/tests/ui/return_impl_future_nonsend.stderr +++ b/embassy-executor/tests/ui/return_impl_future_nonsend.stderr @@ -1,8 +1,8 @@ error: future cannot be sent between threads safely --> tests/ui/return_impl_future_nonsend.rs:18:13 | -18 | s.spawn(task()).unwrap(); - | ^^^^^^ future created by async block is not `Send` +18 | s.spawn(task().unwrap()); + | ^^^^^^^^^^^^^^^ future created by async block is not `Send` | = help: within `impl Sized`, the trait `Send` is not implemented for `*mut ()` note: captured value is not `Send` @@ -13,5 +13,5 @@ note: captured value is not `Send` note: required by a bound in `SendSpawner::spawn` --> src/spawner.rs | - | pub fn spawn(&self, token: SpawnToken) -> Result<(), SpawnError> { + | pub fn spawn(&self, token: SpawnToken) { | ^^^^ required by this bound in `SendSpawner::spawn` diff --git a/embassy-executor/tests/ui/return_impl_send.stderr b/embassy-executor/tests/ui/return_impl_send.stderr index 759be1cde..5d19465ec 100644 --- a/embassy-executor/tests/ui/return_impl_send.stderr +++ b/embassy-executor/tests/ui/return_impl_send.stderr @@ -97,7 +97,7 @@ note: required by a bound in `TaskPool::::spawn` | impl TaskPool { | ^^^^^^ required by this bound in `TaskPool::::spawn` ... - | pub fn spawn(&'static self, future: impl FnOnce() -> F) -> SpawnToken { + | pub fn spawn(&'static self, future: impl FnOnce() -> F) -> Result, SpawnError> { | ----- required by a bound in this associated function = note: this error originates in the attribute macro `embassy_executor::task` (in Nightly builds, run with -Z macro-backtrace for more info) diff --git a/embassy-executor/tests/ui/spawn_nonsend.rs b/embassy-executor/tests/ui/spawn_nonsend.rs index 4c4cc7697..601041941 100644 --- a/embassy-executor/tests/ui/spawn_nonsend.rs +++ b/embassy-executor/tests/ui/spawn_nonsend.rs @@ -10,7 +10,7 @@ async fn task(non_send: *mut ()) { } fn send_spawn(s: SendSpawner) { - s.spawn(task(core::ptr::null_mut())).unwrap(); + s.spawn(task(core::ptr::null_mut()).unwrap()); } fn main() {} diff --git a/embassy-executor/tests/ui/spawn_nonsend.stderr b/embassy-executor/tests/ui/spawn_nonsend.stderr index 2a06c8b94..25bd7d78d 100644 --- a/embassy-executor/tests/ui/spawn_nonsend.stderr +++ b/embassy-executor/tests/ui/spawn_nonsend.stderr @@ -12,8 +12,8 @@ error[E0277]: `*mut ()` cannot be sent between threads safely 7 | #[embassy_executor::task] | ------------------------- within this `impl Sized` ... -13 | s.spawn(task(core::ptr::null_mut())).unwrap(); - | ----- ^^^^^^^^^^^^^^^^^^^^^^^^^^^ `*mut ()` cannot be sent between threads safely +13 | s.spawn(task(core::ptr::null_mut()).unwrap()); + | ----- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `*mut ()` cannot be sent between threads safely | | | required by a bound introduced by this call | @@ -26,8 +26,8 @@ note: required because it's used within this closure note: required because it appears within the type `impl Sized` --> src/raw/mod.rs | - | pub unsafe fn _spawn_async_fn(&'static self, future: FutFn) -> SpawnToken - | ^^^^^^^^^^ + | pub unsafe fn _spawn_async_fn(&'static self, future: FutFn) -> Result, SpawnError> + | ^^^^^^^^^^ note: required because it appears within the type `impl Sized` --> tests/ui/spawn_nonsend.rs:7:1 | @@ -36,6 +36,6 @@ note: required because it appears within the type `impl Sized` note: required by a bound in `SendSpawner::spawn` --> src/spawner.rs | - | pub fn spawn(&self, token: SpawnToken) -> Result<(), SpawnError> { + | pub fn spawn(&self, token: SpawnToken) { | ^^^^ required by this bound in `SendSpawner::spawn` = note: this error originates in the attribute macro `embassy_executor::task` (in Nightly builds, run with -Z macro-backtrace for more info) -- cgit