fix: make LocalWake available in targets that don't support atomics by removing a #[cfg(target_has_atomic = ptr)]

This commit is contained in:
Tomás Vallotton 2023-12-06 15:19:24 -03:00
parent 403718b19d
commit 2012d4b703
3 changed files with 14 additions and 10 deletions

View file

@ -254,7 +254,7 @@ pub mod str;
pub mod string; pub mod string;
#[cfg(all(not(no_rc), not(no_sync), target_has_atomic = "ptr"))] #[cfg(all(not(no_rc), not(no_sync), target_has_atomic = "ptr"))]
pub mod sync; pub mod sync;
#[cfg(all(not(no_global_oom_handling), not(no_rc), not(no_sync), target_has_atomic = "ptr"))] #[cfg(all(not(no_global_oom_handling), not(no_rc), not(no_sync)))]
pub mod task; pub mod task;
#[cfg(test)] #[cfg(test)]
mod tests; mod tests;

View file

@ -2,15 +2,17 @@
//! Types and Traits for working with asynchronous tasks. //! Types and Traits for working with asynchronous tasks.
//! //!
//! **Note**: This module is only available on platforms that support atomic //! **Note**: Some of the types in this module are only available
//! loads and stores of pointers. This may be detected at compile time using //! on platforms that support atomic loads and stores of pointers.
//! This may be detected at compile time using
//! `#[cfg(target_has_atomic = "ptr")]`. //! `#[cfg(target_has_atomic = "ptr")]`.
use core::mem::ManuallyDrop; use core::mem::ManuallyDrop;
use core::task::{LocalWaker, RawWaker, RawWakerVTable, Waker}; use core::task::{LocalWaker, RawWaker, RawWakerVTable};
use crate::rc::Rc; use crate::rc::Rc;
use crate::sync::Arc;
#[cfg(target_has_atomic = "ptr")]
use core::{task::Waker, sync::Arc};
/// The implementation of waking a task on an executor. /// The implementation of waking a task on an executor.
/// ///
@ -74,6 +76,7 @@ use crate::sync::Arc;
/// println!("Hi from inside a future!"); /// println!("Hi from inside a future!");
/// }); /// });
/// ``` /// ```
#[cfg(target_has_atomic = "ptr")]
#[stable(feature = "wake_trait", since = "1.51.0")] #[stable(feature = "wake_trait", since = "1.51.0")]
pub trait Wake { pub trait Wake {
/// Wake this task. /// Wake this task.
@ -92,7 +95,7 @@ pub trait Wake {
self.clone().wake(); self.clone().wake();
} }
} }
#[cfg(target_has_atomic = "ptr")]
#[stable(feature = "wake_trait", since = "1.51.0")] #[stable(feature = "wake_trait", since = "1.51.0")]
impl<W: Wake + Send + Sync + 'static> From<Arc<W>> for Waker { impl<W: Wake + Send + Sync + 'static> From<Arc<W>> for Waker {
/// Use a `Wake`-able type as a `Waker`. /// Use a `Wake`-able type as a `Waker`.
@ -104,7 +107,7 @@ impl<W: Wake + Send + Sync + 'static> From<Arc<W>> for Waker {
unsafe { Waker::from_raw(raw_waker(waker)) } unsafe { Waker::from_raw(raw_waker(waker)) }
} }
} }
#[cfg(target_has_atomic = "ptr")]
#[stable(feature = "wake_trait", since = "1.51.0")] #[stable(feature = "wake_trait", since = "1.51.0")]
impl<W: Wake + Send + Sync + 'static> From<Arc<W>> for RawWaker { impl<W: Wake + Send + Sync + 'static> From<Arc<W>> for RawWaker {
/// Use a `Wake`-able type as a `RawWaker`. /// Use a `Wake`-able type as a `RawWaker`.
@ -120,6 +123,7 @@ impl<W: Wake + Send + Sync + 'static> From<Arc<W>> for RawWaker {
// the safety of `From<Arc<W>> for Waker` does not depend on the correct // the safety of `From<Arc<W>> for Waker` does not depend on the correct
// trait dispatch - instead both impls call this function directly and // trait dispatch - instead both impls call this function directly and
// explicitly. // explicitly.
#[cfg(target_has_atomic = "ptr")]
#[inline(always)] #[inline(always)]
fn raw_waker<W: Wake + Send + Sync + 'static>(waker: Arc<W>) -> RawWaker { fn raw_waker<W: Wake + Send + Sync + 'static>(waker: Arc<W>) -> RawWaker {
// Increment the reference count of the arc to clone it. // Increment the reference count of the arc to clone it.

View file

@ -347,7 +347,7 @@ impl<'a> ContextBuilder<'a> {
/// use std::task::{Waker, ContextBuilder}; /// use std::task::{Waker, ContextBuilder};
/// use std::future::{poll_fn, Future}; /// use std::future::{poll_fn, Future};
/// use std::pin::pin; /// use std::pin::pin;
/// ///
/// async fn with_waker<F>(f: F, waker: &Waker) -> F::Output /// async fn with_waker<F>(f: F, waker: &Waker) -> F::Output
/// where /// where
/// F: Future /// F: Future
@ -365,7 +365,7 @@ impl<'a> ContextBuilder<'a> {
/// f.as_mut().poll(&mut cx) /// f.as_mut().poll(&mut cx)
/// }).await /// }).await
/// } /// }
/// ///
/// # async fn __() { /// # async fn __() {
/// with_waker(async { /* ... */ }, &Waker::noop()).await; /// with_waker(async { /* ... */ }, &Waker::noop()).await;
/// # } /// # }