From 6f847f81b2f540f0a2afb740f871711d15ed37de Mon Sep 17 00:00:00 2001 From: "Enzo \"raskyld\" Nocera" Date: Sat, 19 Oct 2024 13:42:41 +0200 Subject: [PATCH] chore: revert changes to any_spawner Signed-off-by: Enzo "raskyld" Nocera --- any_spawner/src/lib.rs | 57 +++++++----------------------------------- 1 file changed, 9 insertions(+), 48 deletions(-) diff --git a/any_spawner/src/lib.rs b/any_spawner/src/lib.rs index 1fcaee6a8e..a03196a23b 100644 --- a/any_spawner/src/lib.rs +++ b/any_spawner/src/lib.rs @@ -29,9 +29,7 @@ #![deny(missing_docs)] #![cfg_attr(docsrs, feature(doc_cfg))] -use core::{future::Future, panic::Location, pin::Pin}; -use futures::channel::oneshot; -use std::sync::OnceLock; +use std::{future::Future, pin::Pin, sync::OnceLock}; use thiserror::Error; /// A future that has been pinned. @@ -39,18 +37,12 @@ pub type PinnedFuture = Pin + Send>>; /// A future that has been pinned. pub type PinnedLocalFuture = Pin>>; -/// Handle to spawn a new [`PinnedFuture`] on the initiated [`Executor`]. static SPAWN: OnceLock)> = OnceLock::new(); - -/// Handle to spawn a new [`PinnedLocalFuture`] on the initiated [`Executor`]. -/// -/// It is useful when you have a Future that is not [`Send`]. static SPAWN_LOCAL: OnceLock)> = OnceLock::new(); static POLL_LOCAL: OnceLock = OnceLock::new(); /// Errors that can occur when using the executor. #[derive(Error, Debug)] -#[non_exhaustive] pub enum ExecutorError { /// The executor has already been set. #[error("Executor has already been set.")] @@ -70,11 +62,7 @@ impl Executor { /// # } /// ``` #[track_caller] - #[inline] - pub fn spawn(fut: T) - where - T: Future + Send + 'static, - { + pub fn spawn(fut: impl Future + Send + 'static) { if let Some(spawner) = SPAWN.get() { spawner(Box::pin(fut)) } else { @@ -82,13 +70,13 @@ impl Executor { tracing::error!( "At {}, tried to spawn a Future with Executor::spawn() before \ the Executor had been set.", - Location::caller() + std::panic::Location::caller() ); #[cfg(all(debug_assertions, not(feature = "tracing")))] panic!( "At {}, tried to spawn a Future with Executor::spawn() before \ the Executor had been set.", - Location::caller() + std::panic::Location::caller() ); } } @@ -103,11 +91,7 @@ impl Executor { /// # } /// ``` #[track_caller] - #[inline] - pub fn spawn_local(fut: T) - where - T: Future + 'static, - { + pub fn spawn_local(fut: impl Future + 'static) { if let Some(spawner) = SPAWN_LOCAL.get() { spawner(Box::pin(fut)) } else { @@ -115,44 +99,21 @@ impl Executor { tracing::error!( "At {}, tried to spawn a Future with Executor::spawn_local() \ before the Executor had been set.", - Location::caller() + std::panic::Location::caller() ); #[cfg(all(debug_assertions, not(feature = "tracing")))] panic!( "At {}, tried to spawn a Future with Executor::spawn_local() \ before the Executor had been set.", - Location::caller() - ); - } - } - - /// Run the [`Executor`]. - #[track_caller] - #[inline] - pub fn run() { - if let Some(run) = RUN.get() { - run(); - } else { - #[cfg(all(debug_assertions, feature = "tracing"))] - tracing::error!( - "At {}, tried to run an executor with Executor::run() \ - before the Executor had been set.", - Location::caller() - ); - #[cfg(all(debug_assertions, not(feature = "tracing")))] - panic!( - "At {}, tried to run an executor with Executor::run() \ - before the Executor had been set.", - Location::caller() + std::panic::Location::caller() ); } } /// Waits until the next "tick" of the current async executor. - #[inline] pub async fn tick() { - let (tx, rx) = oneshot::channel(); - Self::spawn(async move { + let (tx, rx) = futures::channel::oneshot::channel(); + Executor::spawn(async move { _ = tx.send(()); }); _ = rx.await;