diff --git a/Cargo.toml b/Cargo.toml index c3783f9..622b6e9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,3 +11,10 @@ homepage = "https://github.com/jamesmunns/cassette" repository = "https://github.com/jamesmunns/cassette" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html +[dependencies.futures] +version = "0.4.0-alpha.0" +default-features = false + +[patch.crates-io.futures] +git = "https://github.com/taiki-e/futures-rs" +branch = "cfg_target_has_atomic-2" diff --git a/demo/src/main.rs b/demo/src/main.rs index c8aa286..4c266e4 100644 --- a/demo/src/main.rs +++ b/demo/src/main.rs @@ -7,7 +7,7 @@ use core::{ use cassette::{ Cassette, - pin_mut, + futures::pin_mut, }; struct Demo { diff --git a/src/futures.rs b/src/futures.rs deleted file mode 100644 index 6187a44..0000000 --- a/src/futures.rs +++ /dev/null @@ -1,103 +0,0 @@ -//! Items pulled from the `futures` crate -//! -//! This module is necessary due to a lack of ability to disable -//! the use of atomic CAS operations in the futures crate. Eventually -//! this module should go away. -// -// This module includes code licensed under the MIT+Apache2.0 dual -// licenses from the `futures-rs` crate. Please see the upstream -// repository at https://github.com/rust-lang/futures-rs, and details -// of the license here: https://github.com/rust-lang/futures-rs#license - -use core::fmt; -use core::pin::Pin; -use core::future::Future; -use core::task::{Context, Poll}; - -/// Pins a value on the stack. -/// -/// NOTE: Taken from `futures::pin_mut!()` -/// -/// # Example -/// -/// ```rust -/// # use cassette::pin_mut; -/// # use core::pin::Pin; -/// # struct Foo {} -/// let foo = Foo { /* ... */ }; -/// pin_mut!(foo); -/// let _: Pin<&mut Foo> = foo; -/// ``` -#[macro_export] -macro_rules! pin_mut { - ($($x:ident),* $(,)?) => { $( - // Move the value to ensure that it is owned - let mut $x = $x; - // Shadow the original binding so that it can't be directly accessed - // ever again. - #[allow(unused_mut)] - let mut $x = unsafe { - core::pin::Pin::new_unchecked(&mut $x) - }; - )* } -} - - -// Just a helper function to ensure the futures we're returning all have the -// right implementations. -pub(crate) fn assert_future(future: F) -> F -where - F: Future, -{ - future -} - -/// Future for the [`poll_fn`] function. -#[must_use = "futures do nothing unless you `.await` or poll them"] -pub struct PollFn { - f: F, -} - -impl Unpin for PollFn {} - -/// Creates a new future wrapping around a function returning [`Poll`]. -/// -/// Polling the returned future delegates to the wrapped function. -/// -/// # Examples -/// -/// ```no_run -/// use cassette::futures::poll_fn; -/// use core::task::{Context, Poll}; -/// -/// fn read_line(_cx: &mut Context<'_>) -> Poll { -/// Poll::Ready("Hello, World!".into()) -/// } -/// -/// # async fn func() { -/// let read_future = poll_fn(read_line); -/// assert_eq!(read_future.await, "Hello, World!".to_owned()); -/// # } -/// ``` -pub fn poll_fn(f: F) -> PollFn -where - F: FnMut(&mut Context<'_>) -> Poll -{ - assert_future::(PollFn { f }) -} - -impl fmt::Debug for PollFn { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.debug_struct("PollFn").finish() - } -} - -impl Future for PollFn - where F: FnMut(&mut Context<'_>) -> Poll, -{ - type Output = T; - - fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { - (&mut self.f)(cx) - } -} diff --git a/src/lib.rs b/src/lib.rs index 579d220..93f1ff1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -222,7 +222,7 @@ use core::{ pin::Pin, task::{Context, Poll, RawWaker, RawWakerVTable, Waker}, }; -pub mod futures; +pub use futures; fn no_op(_: *const ()) {} fn no_op_clone(_: *const()) -> RawWaker { noop_raw_waker() }