summaryrefslogtreecommitdiffstats
path: root/third_party/rust/futures-task/src
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 00:47:55 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 00:47:55 +0000
commit26a029d407be480d791972afb5975cf62c9360a6 (patch)
treef435a8308119effd964b339f76abb83a57c29483 /third_party/rust/futures-task/src
parentInitial commit. (diff)
downloadfirefox-26a029d407be480d791972afb5975cf62c9360a6.tar.xz
firefox-26a029d407be480d791972afb5975cf62c9360a6.zip
Adding upstream version 124.0.1.upstream/124.0.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/futures-task/src')
-rw-r--r--third_party/rust/futures-task/src/arc_wake.rs49
-rw-r--r--third_party/rust/futures-task/src/future_obj.rs335
-rw-r--r--third_party/rust/futures-task/src/lib.rs50
-rw-r--r--third_party/rust/futures-task/src/noop_waker.rs63
-rw-r--r--third_party/rust/futures-task/src/spawn.rs192
-rw-r--r--third_party/rust/futures-task/src/waker.rs59
-rw-r--r--third_party/rust/futures-task/src/waker_ref.rs66
7 files changed, 814 insertions, 0 deletions
diff --git a/third_party/rust/futures-task/src/arc_wake.rs b/third_party/rust/futures-task/src/arc_wake.rs
new file mode 100644
index 0000000000..aa6de0fc43
--- /dev/null
+++ b/third_party/rust/futures-task/src/arc_wake.rs
@@ -0,0 +1,49 @@
+use alloc::sync::Arc;
+
+/// A way of waking up a specific task.
+///
+/// By implementing this trait, types that are expected to be wrapped in an `Arc`
+/// can be converted into [`Waker`] objects.
+/// Those Wakers can be used to signal executors that a task it owns
+/// is ready to be `poll`ed again.
+///
+/// Currently, there are two ways to convert `ArcWake` into [`Waker`]:
+///
+/// * [`waker`](super::waker()) converts `Arc<impl ArcWake>` into [`Waker`].
+/// * [`waker_ref`](super::waker_ref()) converts `&Arc<impl ArcWake>` into [`WakerRef`] that
+/// provides access to a [`&Waker`][`Waker`].
+///
+/// [`Waker`]: std::task::Waker
+/// [`WakerRef`]: super::WakerRef
+// Note: Send + Sync required because `Arc<T>` doesn't automatically imply
+// those bounds, but `Waker` implements them.
+pub trait ArcWake: Send + Sync {
+ /// Indicates that the associated task is ready to make progress and should
+ /// be `poll`ed.
+ ///
+ /// This function can be called from an arbitrary thread, including threads which
+ /// did not create the `ArcWake` based [`Waker`].
+ ///
+ /// Executors generally maintain a queue of "ready" tasks; `wake` should place
+ /// the associated task onto this queue.
+ ///
+ /// [`Waker`]: std::task::Waker
+ fn wake(self: Arc<Self>) {
+ Self::wake_by_ref(&self)
+ }
+
+ /// Indicates that the associated task is ready to make progress and should
+ /// be `poll`ed.
+ ///
+ /// This function can be called from an arbitrary thread, including threads which
+ /// did not create the `ArcWake` based [`Waker`].
+ ///
+ /// Executors generally maintain a queue of "ready" tasks; `wake_by_ref` should place
+ /// the associated task onto this queue.
+ ///
+ /// This function is similar to [`wake`](ArcWake::wake), but must not consume the provided data
+ /// pointer.
+ ///
+ /// [`Waker`]: std::task::Waker
+ fn wake_by_ref(arc_self: &Arc<Self>);
+}
diff --git a/third_party/rust/futures-task/src/future_obj.rs b/third_party/rust/futures-task/src/future_obj.rs
new file mode 100644
index 0000000000..071392af6c
--- /dev/null
+++ b/third_party/rust/futures-task/src/future_obj.rs
@@ -0,0 +1,335 @@
+use core::{
+ fmt,
+ future::Future,
+ marker::PhantomData,
+ mem,
+ pin::Pin,
+ task::{Context, Poll},
+};
+
+/// A custom trait object for polling futures, roughly akin to
+/// `Box<dyn Future<Output = T> + 'a>`.
+///
+/// This custom trait object was introduced as currently it is not possible to
+/// take `dyn Trait` by value and `Box<dyn Trait>` is not available in no_std
+/// contexts.
+pub struct LocalFutureObj<'a, T> {
+ future: *mut (dyn Future<Output = T> + 'static),
+ drop_fn: unsafe fn(*mut (dyn Future<Output = T> + 'static)),
+ _marker: PhantomData<&'a ()>,
+}
+
+// As LocalFutureObj only holds pointers, even if we move it, the pointed to values won't move,
+// so this is safe as long as we don't provide any way for a user to directly access the pointers
+// and move their values.
+impl<T> Unpin for LocalFutureObj<'_, T> {}
+
+#[allow(single_use_lifetimes)]
+#[allow(clippy::transmute_ptr_to_ptr)]
+unsafe fn remove_future_lifetime<'a, T>(
+ ptr: *mut (dyn Future<Output = T> + 'a),
+) -> *mut (dyn Future<Output = T> + 'static) {
+ mem::transmute(ptr)
+}
+
+#[allow(single_use_lifetimes)]
+unsafe fn remove_drop_lifetime<'a, T>(
+ ptr: unsafe fn(*mut (dyn Future<Output = T> + 'a)),
+) -> unsafe fn(*mut (dyn Future<Output = T> + 'static)) {
+ mem::transmute(ptr)
+}
+
+impl<'a, T> LocalFutureObj<'a, T> {
+ /// Create a `LocalFutureObj` from a custom trait object representation.
+ #[inline]
+ pub fn new<F: UnsafeFutureObj<'a, T> + 'a>(f: F) -> Self {
+ Self {
+ future: unsafe { remove_future_lifetime(f.into_raw()) },
+ drop_fn: unsafe { remove_drop_lifetime(F::drop) },
+ _marker: PhantomData,
+ }
+ }
+
+ /// Converts the `LocalFutureObj` into a `FutureObj`.
+ ///
+ /// # Safety
+ ///
+ /// To make this operation safe one has to ensure that the `UnsafeFutureObj`
+ /// instance from which this `LocalFutureObj` was created actually
+ /// implements `Send`.
+ #[inline]
+ pub unsafe fn into_future_obj(self) -> FutureObj<'a, T> {
+ FutureObj(self)
+ }
+}
+
+impl<T> fmt::Debug for LocalFutureObj<'_, T> {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ f.debug_struct("LocalFutureObj").finish()
+ }
+}
+
+impl<'a, T> From<FutureObj<'a, T>> for LocalFutureObj<'a, T> {
+ #[inline]
+ fn from(f: FutureObj<'a, T>) -> Self {
+ f.0
+ }
+}
+
+impl<T> Future for LocalFutureObj<'_, T> {
+ type Output = T;
+
+ #[inline]
+ fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<T> {
+ unsafe { Pin::new_unchecked(&mut *self.future).poll(cx) }
+ }
+}
+
+impl<T> Drop for LocalFutureObj<'_, T> {
+ fn drop(&mut self) {
+ unsafe { (self.drop_fn)(self.future) }
+ }
+}
+
+/// A custom trait object for polling futures, roughly akin to
+/// `Box<dyn Future<Output = T> + Send + 'a>`.
+///
+/// This custom trait object was introduced as currently it is not possible to
+/// take `dyn Trait` by value and `Box<dyn Trait>` is not available in no_std
+/// contexts.
+///
+/// You should generally not need to use this type outside of `no_std` or when
+/// implementing `Spawn`, consider using `BoxFuture` instead.
+pub struct FutureObj<'a, T>(LocalFutureObj<'a, T>);
+
+impl<T> Unpin for FutureObj<'_, T> {}
+unsafe impl<T> Send for FutureObj<'_, T> {}
+
+impl<'a, T> FutureObj<'a, T> {
+ /// Create a `FutureObj` from a custom trait object representation.
+ #[inline]
+ pub fn new<F: UnsafeFutureObj<'a, T> + Send>(f: F) -> Self {
+ Self(LocalFutureObj::new(f))
+ }
+}
+
+impl<T> fmt::Debug for FutureObj<'_, T> {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ f.debug_struct("FutureObj").finish()
+ }
+}
+
+impl<T> Future for FutureObj<'_, T> {
+ type Output = T;
+
+ #[inline]
+ fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<T> {
+ Pin::new(&mut self.0).poll(cx)
+ }
+}
+
+/// A custom implementation of a future trait object for `FutureObj`, providing
+/// a vtable with drop support.
+///
+/// This custom representation is typically used only in `no_std` contexts,
+/// where the default `Box`-based implementation is not available.
+///
+/// # Safety
+///
+/// See the safety notes on individual methods for what guarantees an
+/// implementor must provide.
+pub unsafe trait UnsafeFutureObj<'a, T>: 'a {
+ /// Convert an owned instance into a (conceptually owned) fat pointer.
+ ///
+ /// # Safety
+ ///
+ /// ## Implementor
+ ///
+ /// The trait implementor must guarantee that it is safe to convert the
+ /// provided `*mut (dyn Future<Output = T> + 'a)` into a `Pin<&mut (dyn
+ /// Future<Output = T> + 'a)>` and call methods on it, non-reentrantly,
+ /// until `UnsafeFutureObj::drop` is called with it.
+ #[allow(clippy::unnecessary_safety_doc)]
+ fn into_raw(self) -> *mut (dyn Future<Output = T> + 'a);
+
+ /// Drops the future represented by the given fat pointer.
+ ///
+ /// # Safety
+ ///
+ /// ## Implementor
+ ///
+ /// The trait implementor must guarantee that it is safe to call this
+ /// function once per `into_raw` invocation.
+ ///
+ /// ## Caller
+ ///
+ /// The caller must ensure:
+ ///
+ /// * the pointer passed was obtained from an `into_raw` invocation from
+ /// this same trait object
+ /// * the pointer is not currently in use as a `Pin<&mut (dyn Future<Output
+ /// = T> + 'a)>`
+ /// * the pointer must not be used again after this function is called
+ unsafe fn drop(ptr: *mut (dyn Future<Output = T> + 'a));
+}
+
+unsafe impl<'a, T, F> UnsafeFutureObj<'a, T> for &'a mut F
+where
+ F: Future<Output = T> + Unpin + 'a,
+{
+ fn into_raw(self) -> *mut (dyn Future<Output = T> + 'a) {
+ self as *mut dyn Future<Output = T>
+ }
+
+ unsafe fn drop(_ptr: *mut (dyn Future<Output = T> + 'a)) {}
+}
+
+unsafe impl<'a, T> UnsafeFutureObj<'a, T> for &'a mut (dyn Future<Output = T> + Unpin + 'a) {
+ fn into_raw(self) -> *mut (dyn Future<Output = T> + 'a) {
+ self as *mut dyn Future<Output = T>
+ }
+
+ unsafe fn drop(_ptr: *mut (dyn Future<Output = T> + 'a)) {}
+}
+
+unsafe impl<'a, T, F> UnsafeFutureObj<'a, T> for Pin<&'a mut F>
+where
+ F: Future<Output = T> + 'a,
+{
+ fn into_raw(self) -> *mut (dyn Future<Output = T> + 'a) {
+ unsafe { self.get_unchecked_mut() as *mut dyn Future<Output = T> }
+ }
+
+ unsafe fn drop(_ptr: *mut (dyn Future<Output = T> + 'a)) {}
+}
+
+unsafe impl<'a, T> UnsafeFutureObj<'a, T> for Pin<&'a mut (dyn Future<Output = T> + 'a)> {
+ fn into_raw(self) -> *mut (dyn Future<Output = T> + 'a) {
+ unsafe { self.get_unchecked_mut() as *mut dyn Future<Output = T> }
+ }
+
+ unsafe fn drop(_ptr: *mut (dyn Future<Output = T> + 'a)) {}
+}
+
+#[cfg(feature = "alloc")]
+mod if_alloc {
+ use super::*;
+ use alloc::boxed::Box;
+
+ unsafe impl<'a, T, F> UnsafeFutureObj<'a, T> for Box<F>
+ where
+ F: Future<Output = T> + 'a,
+ {
+ fn into_raw(self) -> *mut (dyn Future<Output = T> + 'a) {
+ Box::into_raw(self)
+ }
+
+ unsafe fn drop(ptr: *mut (dyn Future<Output = T> + 'a)) {
+ drop(Box::from_raw(ptr.cast::<F>()))
+ }
+ }
+
+ unsafe impl<'a, T: 'a> UnsafeFutureObj<'a, T> for Box<dyn Future<Output = T> + 'a> {
+ fn into_raw(self) -> *mut (dyn Future<Output = T> + 'a) {
+ Box::into_raw(self)
+ }
+
+ unsafe fn drop(ptr: *mut (dyn Future<Output = T> + 'a)) {
+ drop(Box::from_raw(ptr))
+ }
+ }
+
+ unsafe impl<'a, T: 'a> UnsafeFutureObj<'a, T> for Box<dyn Future<Output = T> + Send + 'a> {
+ fn into_raw(self) -> *mut (dyn Future<Output = T> + 'a) {
+ Box::into_raw(self)
+ }
+
+ unsafe fn drop(ptr: *mut (dyn Future<Output = T> + 'a)) {
+ drop(Box::from_raw(ptr))
+ }
+ }
+
+ unsafe impl<'a, T, F> UnsafeFutureObj<'a, T> for Pin<Box<F>>
+ where
+ F: Future<Output = T> + 'a,
+ {
+ fn into_raw(self) -> *mut (dyn Future<Output = T> + 'a) {
+ let mut this = mem::ManuallyDrop::new(self);
+ unsafe { this.as_mut().get_unchecked_mut() as *mut _ }
+ }
+
+ unsafe fn drop(ptr: *mut (dyn Future<Output = T> + 'a)) {
+ drop(Pin::from(Box::from_raw(ptr)))
+ }
+ }
+
+ unsafe impl<'a, T: 'a> UnsafeFutureObj<'a, T> for Pin<Box<dyn Future<Output = T> + 'a>> {
+ fn into_raw(self) -> *mut (dyn Future<Output = T> + 'a) {
+ let mut this = mem::ManuallyDrop::new(self);
+ unsafe { this.as_mut().get_unchecked_mut() as *mut _ }
+ }
+
+ unsafe fn drop(ptr: *mut (dyn Future<Output = T> + 'a)) {
+ drop(Pin::from(Box::from_raw(ptr)))
+ }
+ }
+
+ unsafe impl<'a, T: 'a> UnsafeFutureObj<'a, T> for Pin<Box<dyn Future<Output = T> + Send + 'a>> {
+ fn into_raw(self) -> *mut (dyn Future<Output = T> + 'a) {
+ let mut this = mem::ManuallyDrop::new(self);
+ unsafe { this.as_mut().get_unchecked_mut() as *mut _ }
+ }
+
+ unsafe fn drop(ptr: *mut (dyn Future<Output = T> + 'a)) {
+ drop(Pin::from(Box::from_raw(ptr)))
+ }
+ }
+
+ impl<'a, F: Future<Output = ()> + Send + 'a> From<Box<F>> for FutureObj<'a, ()> {
+ fn from(boxed: Box<F>) -> Self {
+ Self::new(boxed)
+ }
+ }
+
+ impl<'a> From<Box<dyn Future<Output = ()> + Send + 'a>> for FutureObj<'a, ()> {
+ fn from(boxed: Box<dyn Future<Output = ()> + Send + 'a>) -> Self {
+ Self::new(boxed)
+ }
+ }
+
+ impl<'a, F: Future<Output = ()> + Send + 'a> From<Pin<Box<F>>> for FutureObj<'a, ()> {
+ fn from(boxed: Pin<Box<F>>) -> Self {
+ Self::new(boxed)
+ }
+ }
+
+ impl<'a> From<Pin<Box<dyn Future<Output = ()> + Send + 'a>>> for FutureObj<'a, ()> {
+ fn from(boxed: Pin<Box<dyn Future<Output = ()> + Send + 'a>>) -> Self {
+ Self::new(boxed)
+ }
+ }
+
+ impl<'a, F: Future<Output = ()> + 'a> From<Box<F>> for LocalFutureObj<'a, ()> {
+ fn from(boxed: Box<F>) -> Self {
+ Self::new(boxed)
+ }
+ }
+
+ impl<'a> From<Box<dyn Future<Output = ()> + 'a>> for LocalFutureObj<'a, ()> {
+ fn from(boxed: Box<dyn Future<Output = ()> + 'a>) -> Self {
+ Self::new(boxed)
+ }
+ }
+
+ impl<'a, F: Future<Output = ()> + 'a> From<Pin<Box<F>>> for LocalFutureObj<'a, ()> {
+ fn from(boxed: Pin<Box<F>>) -> Self {
+ Self::new(boxed)
+ }
+ }
+
+ impl<'a> From<Pin<Box<dyn Future<Output = ()> + 'a>>> for LocalFutureObj<'a, ()> {
+ fn from(boxed: Pin<Box<dyn Future<Output = ()> + 'a>>) -> Self {
+ Self::new(boxed)
+ }
+ }
+}
diff --git a/third_party/rust/futures-task/src/lib.rs b/third_party/rust/futures-task/src/lib.rs
new file mode 100644
index 0000000000..c72460744c
--- /dev/null
+++ b/third_party/rust/futures-task/src/lib.rs
@@ -0,0 +1,50 @@
+//! Tools for working with tasks.
+
+#![cfg_attr(not(feature = "std"), no_std)]
+#![warn(missing_debug_implementations, missing_docs, rust_2018_idioms, unreachable_pub)]
+// It cannot be included in the published code because this lints have false positives in the minimum required version.
+#![cfg_attr(test, warn(single_use_lifetimes))]
+#![doc(test(
+ no_crate_inject,
+ attr(
+ deny(warnings, rust_2018_idioms, single_use_lifetimes),
+ allow(dead_code, unused_assignments, unused_variables)
+ )
+))]
+
+#[cfg(feature = "alloc")]
+extern crate alloc;
+
+mod spawn;
+pub use crate::spawn::{LocalSpawn, Spawn, SpawnError};
+
+#[cfg(not(futures_no_atomic_cas))]
+#[cfg(feature = "alloc")]
+mod arc_wake;
+#[cfg(not(futures_no_atomic_cas))]
+#[cfg(feature = "alloc")]
+pub use crate::arc_wake::ArcWake;
+
+#[cfg(not(futures_no_atomic_cas))]
+#[cfg(feature = "alloc")]
+mod waker;
+#[cfg(not(futures_no_atomic_cas))]
+#[cfg(feature = "alloc")]
+pub use crate::waker::waker;
+
+#[cfg(not(futures_no_atomic_cas))]
+#[cfg(feature = "alloc")]
+mod waker_ref;
+#[cfg(not(futures_no_atomic_cas))]
+#[cfg(feature = "alloc")]
+pub use crate::waker_ref::{waker_ref, WakerRef};
+
+mod future_obj;
+pub use crate::future_obj::{FutureObj, LocalFutureObj, UnsafeFutureObj};
+
+mod noop_waker;
+pub use crate::noop_waker::noop_waker;
+pub use crate::noop_waker::noop_waker_ref;
+
+#[doc(no_inline)]
+pub use core::task::{Context, Poll, RawWaker, RawWakerVTable, Waker};
diff --git a/third_party/rust/futures-task/src/noop_waker.rs b/third_party/rust/futures-task/src/noop_waker.rs
new file mode 100644
index 0000000000..f76a8a2e95
--- /dev/null
+++ b/third_party/rust/futures-task/src/noop_waker.rs
@@ -0,0 +1,63 @@
+//! Utilities for creating zero-cost wakers that don't do anything.
+
+use core::ptr::null;
+use core::task::{RawWaker, RawWakerVTable, Waker};
+
+unsafe fn noop_clone(_data: *const ()) -> RawWaker {
+ noop_raw_waker()
+}
+
+unsafe fn noop(_data: *const ()) {}
+
+const NOOP_WAKER_VTABLE: RawWakerVTable = RawWakerVTable::new(noop_clone, noop, noop, noop);
+
+const fn noop_raw_waker() -> RawWaker {
+ RawWaker::new(null(), &NOOP_WAKER_VTABLE)
+}
+
+/// Create a new [`Waker`] which does
+/// nothing when `wake()` is called on it.
+///
+/// # Examples
+///
+/// ```
+/// use futures::task::noop_waker;
+/// let waker = noop_waker();
+/// waker.wake();
+/// ```
+#[inline]
+pub fn noop_waker() -> Waker {
+ // FIXME: Since 1.46.0 we can use transmute in consts, allowing this function to be const.
+ unsafe { Waker::from_raw(noop_raw_waker()) }
+}
+
+/// Get a static reference to a [`Waker`] which
+/// does nothing when `wake()` is called on it.
+///
+/// # Examples
+///
+/// ```
+/// use futures::task::noop_waker_ref;
+/// let waker = noop_waker_ref();
+/// waker.wake_by_ref();
+/// ```
+#[inline]
+pub fn noop_waker_ref() -> &'static Waker {
+ struct SyncRawWaker(RawWaker);
+ unsafe impl Sync for SyncRawWaker {}
+
+ static NOOP_WAKER_INSTANCE: SyncRawWaker = SyncRawWaker(noop_raw_waker());
+
+ // SAFETY: `Waker` is #[repr(transparent)] over its `RawWaker`.
+ unsafe { &*(&NOOP_WAKER_INSTANCE.0 as *const RawWaker as *const Waker) }
+}
+
+#[cfg(test)]
+mod tests {
+ #[test]
+ #[cfg(feature = "std")]
+ fn issue_2091_cross_thread_segfault() {
+ let waker = std::thread::spawn(super::noop_waker_ref).join().unwrap();
+ waker.wake_by_ref();
+ }
+}
diff --git a/third_party/rust/futures-task/src/spawn.rs b/third_party/rust/futures-task/src/spawn.rs
new file mode 100644
index 0000000000..f4e63397bd
--- /dev/null
+++ b/third_party/rust/futures-task/src/spawn.rs
@@ -0,0 +1,192 @@
+use crate::{FutureObj, LocalFutureObj};
+use core::fmt;
+
+/// The `Spawn` trait allows for pushing futures onto an executor that will
+/// run them to completion.
+pub trait Spawn {
+ /// Spawns a future that will be run to completion.
+ ///
+ /// # Errors
+ ///
+ /// The executor may be unable to spawn tasks. Spawn errors should
+ /// 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(&self, future: FutureObj<'static, ()>) -> Result<(), SpawnError>;
+
+ /// Determines whether the executor is able to spawn new tasks.
+ ///
+ /// This method will return `Ok` when the executor is *likely*
+ /// (but not guaranteed) to accept a subsequent spawn attempt.
+ /// Likewise, an `Err` return means that `spawn` is likely, but
+ /// not guaranteed, to yield an error.
+ #[inline]
+ fn status(&self) -> Result<(), SpawnError> {
+ Ok(())
+ }
+}
+
+/// The `LocalSpawn` is similar to [`Spawn`], but allows spawning futures
+/// that don't implement `Send`.
+pub trait LocalSpawn {
+ /// Spawns a future that will be run to completion.
+ ///
+ /// # Errors
+ ///
+ /// The executor may be unable to spawn tasks. Spawn errors should
+ /// 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(&self, future: LocalFutureObj<'static, ()>) -> Result<(), SpawnError>;
+
+ /// Determines whether the executor is able to spawn new tasks.
+ ///
+ /// This method will return `Ok` when the executor is *likely*
+ /// (but not guaranteed) to accept a subsequent spawn attempt.
+ /// Likewise, an `Err` return means that `spawn` is likely, but
+ /// not guaranteed, to yield an error.
+ #[inline]
+ fn status_local(&self) -> Result<(), SpawnError> {
+ Ok(())
+ }
+}
+
+/// An error that occurred during spawning.
+pub struct SpawnError {
+ _priv: (),
+}
+
+impl fmt::Debug for SpawnError {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ f.debug_tuple("SpawnError").field(&"shutdown").finish()
+ }
+}
+
+impl fmt::Display for SpawnError {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ write!(f, "Executor is shutdown")
+ }
+}
+
+#[cfg(feature = "std")]
+impl std::error::Error for SpawnError {}
+
+impl SpawnError {
+ /// Spawning failed because the executor has been shut down.
+ pub fn shutdown() -> Self {
+ Self { _priv: () }
+ }
+
+ /// Check whether spawning failed to the executor being shut down.
+ pub fn is_shutdown(&self) -> bool {
+ true
+ }
+}
+
+impl<Sp: ?Sized + Spawn> Spawn for &Sp {
+ fn spawn_obj(&self, future: FutureObj<'static, ()>) -> Result<(), SpawnError> {
+ Sp::spawn_obj(self, future)
+ }
+
+ fn status(&self) -> Result<(), SpawnError> {
+ Sp::status(self)
+ }
+}
+
+impl<Sp: ?Sized + Spawn> Spawn for &mut Sp {
+ fn spawn_obj(&self, future: FutureObj<'static, ()>) -> Result<(), SpawnError> {
+ Sp::spawn_obj(self, future)
+ }
+
+ fn status(&self) -> Result<(), SpawnError> {
+ Sp::status(self)
+ }
+}
+
+impl<Sp: ?Sized + LocalSpawn> LocalSpawn for &Sp {
+ fn spawn_local_obj(&self, future: LocalFutureObj<'static, ()>) -> Result<(), SpawnError> {
+ Sp::spawn_local_obj(self, future)
+ }
+
+ fn status_local(&self) -> Result<(), SpawnError> {
+ Sp::status_local(self)
+ }
+}
+
+impl<Sp: ?Sized + LocalSpawn> LocalSpawn for &mut Sp {
+ fn spawn_local_obj(&self, future: LocalFutureObj<'static, ()>) -> Result<(), SpawnError> {
+ Sp::spawn_local_obj(self, future)
+ }
+
+ fn status_local(&self) -> Result<(), SpawnError> {
+ Sp::status_local(self)
+ }
+}
+
+#[cfg(feature = "alloc")]
+mod if_alloc {
+ use super::*;
+ use alloc::{boxed::Box, rc::Rc};
+
+ impl<Sp: ?Sized + Spawn> Spawn for Box<Sp> {
+ fn spawn_obj(&self, future: FutureObj<'static, ()>) -> Result<(), SpawnError> {
+ (**self).spawn_obj(future)
+ }
+
+ fn status(&self) -> Result<(), SpawnError> {
+ (**self).status()
+ }
+ }
+
+ impl<Sp: ?Sized + LocalSpawn> LocalSpawn for Box<Sp> {
+ fn spawn_local_obj(&self, future: LocalFutureObj<'static, ()>) -> Result<(), SpawnError> {
+ (**self).spawn_local_obj(future)
+ }
+
+ fn status_local(&self) -> Result<(), SpawnError> {
+ (**self).status_local()
+ }
+ }
+
+ impl<Sp: ?Sized + Spawn> Spawn for Rc<Sp> {
+ fn spawn_obj(&self, future: FutureObj<'static, ()>) -> Result<(), SpawnError> {
+ (**self).spawn_obj(future)
+ }
+
+ fn status(&self) -> Result<(), SpawnError> {
+ (**self).status()
+ }
+ }
+
+ impl<Sp: ?Sized + LocalSpawn> LocalSpawn for Rc<Sp> {
+ fn spawn_local_obj(&self, future: LocalFutureObj<'static, ()>) -> Result<(), SpawnError> {
+ (**self).spawn_local_obj(future)
+ }
+
+ fn status_local(&self) -> Result<(), SpawnError> {
+ (**self).status_local()
+ }
+ }
+
+ #[cfg(not(futures_no_atomic_cas))]
+ impl<Sp: ?Sized + Spawn> Spawn for alloc::sync::Arc<Sp> {
+ fn spawn_obj(&self, future: FutureObj<'static, ()>) -> Result<(), SpawnError> {
+ (**self).spawn_obj(future)
+ }
+
+ fn status(&self) -> Result<(), SpawnError> {
+ (**self).status()
+ }
+ }
+
+ #[cfg(not(futures_no_atomic_cas))]
+ impl<Sp: ?Sized + LocalSpawn> LocalSpawn for alloc::sync::Arc<Sp> {
+ fn spawn_local_obj(&self, future: LocalFutureObj<'static, ()>) -> Result<(), SpawnError> {
+ (**self).spawn_local_obj(future)
+ }
+
+ fn status_local(&self) -> Result<(), SpawnError> {
+ (**self).status_local()
+ }
+ }
+}
diff --git a/third_party/rust/futures-task/src/waker.rs b/third_party/rust/futures-task/src/waker.rs
new file mode 100644
index 0000000000..79112569c5
--- /dev/null
+++ b/third_party/rust/futures-task/src/waker.rs
@@ -0,0 +1,59 @@
+use super::arc_wake::ArcWake;
+use alloc::sync::Arc;
+use core::mem;
+use core::task::{RawWaker, RawWakerVTable, Waker};
+
+pub(super) fn waker_vtable<W: ArcWake>() -> &'static RawWakerVTable {
+ &RawWakerVTable::new(
+ clone_arc_raw::<W>,
+ wake_arc_raw::<W>,
+ wake_by_ref_arc_raw::<W>,
+ drop_arc_raw::<W>,
+ )
+}
+
+/// Creates a [`Waker`] from an `Arc<impl ArcWake>`.
+///
+/// The returned [`Waker`] will call
+/// [`ArcWake.wake()`](ArcWake::wake) if awoken.
+pub fn waker<W>(wake: Arc<W>) -> Waker
+where
+ W: ArcWake + 'static,
+{
+ let ptr = Arc::into_raw(wake).cast::<()>();
+
+ unsafe { Waker::from_raw(RawWaker::new(ptr, waker_vtable::<W>())) }
+}
+
+// FIXME: panics on Arc::clone / refcount changes could wreak havoc on the
+// code here. We should guard against this by aborting.
+
+#[allow(clippy::redundant_clone)] // The clone here isn't actually redundant.
+unsafe fn increase_refcount<T: ArcWake>(data: *const ()) {
+ // Retain Arc, but don't touch refcount by wrapping in ManuallyDrop
+ let arc = mem::ManuallyDrop::new(Arc::<T>::from_raw(data.cast::<T>()));
+ // Now increase refcount, but don't drop new refcount either
+ let _arc_clone: mem::ManuallyDrop<_> = arc.clone();
+}
+
+// used by `waker_ref`
+unsafe fn clone_arc_raw<T: ArcWake>(data: *const ()) -> RawWaker {
+ increase_refcount::<T>(data);
+ RawWaker::new(data, waker_vtable::<T>())
+}
+
+unsafe fn wake_arc_raw<T: ArcWake>(data: *const ()) {
+ let arc: Arc<T> = Arc::from_raw(data.cast::<T>());
+ ArcWake::wake(arc);
+}
+
+// used by `waker_ref`
+unsafe fn wake_by_ref_arc_raw<T: ArcWake>(data: *const ()) {
+ // Retain Arc, but don't touch refcount by wrapping in ManuallyDrop
+ let arc = mem::ManuallyDrop::new(Arc::<T>::from_raw(data.cast::<T>()));
+ ArcWake::wake_by_ref(&arc);
+}
+
+unsafe fn drop_arc_raw<T: ArcWake>(data: *const ()) {
+ drop(Arc::<T>::from_raw(data.cast::<T>()))
+}
diff --git a/third_party/rust/futures-task/src/waker_ref.rs b/third_party/rust/futures-task/src/waker_ref.rs
new file mode 100644
index 0000000000..aac4109577
--- /dev/null
+++ b/third_party/rust/futures-task/src/waker_ref.rs
@@ -0,0 +1,66 @@
+use super::arc_wake::ArcWake;
+use super::waker::waker_vtable;
+use alloc::sync::Arc;
+use core::marker::PhantomData;
+use core::mem::ManuallyDrop;
+use core::ops::Deref;
+use core::task::{RawWaker, Waker};
+
+/// A [`Waker`] that is only valid for a given lifetime.
+///
+/// Note: this type implements [`Deref<Target = Waker>`](std::ops::Deref),
+/// so it can be used to get a `&Waker`.
+#[derive(Debug)]
+pub struct WakerRef<'a> {
+ waker: ManuallyDrop<Waker>,
+ _marker: PhantomData<&'a ()>,
+}
+
+impl<'a> WakerRef<'a> {
+ /// Create a new [`WakerRef`] from a [`Waker`] reference.
+ #[inline]
+ pub fn new(waker: &'a Waker) -> Self {
+ // copy the underlying (raw) waker without calling a clone,
+ // as we won't call Waker::drop either.
+ let waker = ManuallyDrop::new(unsafe { core::ptr::read(waker) });
+ Self { waker, _marker: PhantomData }
+ }
+
+ /// Create a new [`WakerRef`] from a [`Waker`] that must not be dropped.
+ ///
+ /// Note: this if for rare cases where the caller created a [`Waker`] in
+ /// an unsafe way (that will be valid only for a lifetime to be determined
+ /// by the caller), and the [`Waker`] doesn't need to or must not be
+ /// destroyed.
+ #[inline]
+ pub fn new_unowned(waker: ManuallyDrop<Waker>) -> Self {
+ Self { waker, _marker: PhantomData }
+ }
+}
+
+impl Deref for WakerRef<'_> {
+ type Target = Waker;
+
+ #[inline]
+ fn deref(&self) -> &Waker {
+ &self.waker
+ }
+}
+
+/// Creates a reference to a [`Waker`] from a reference to `Arc<impl ArcWake>`.
+///
+/// The resulting [`Waker`] will call
+/// [`ArcWake.wake()`](ArcWake::wake) if awoken.
+#[inline]
+pub fn waker_ref<W>(wake: &Arc<W>) -> WakerRef<'_>
+where
+ W: ArcWake,
+{
+ // simply copy the pointer instead of using Arc::into_raw,
+ // as we don't actually keep a refcount by using ManuallyDrop.<
+ let ptr = Arc::as_ptr(wake).cast::<()>();
+
+ let waker =
+ ManuallyDrop::new(unsafe { Waker::from_raw(RawWaker::new(ptr, waker_vtable::<W>())) });
+ WakerRef::new_unowned(waker)
+}