diff --git a/Cargo.toml b/Cargo.toml index 93e28e7..6fffa06 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,3 +14,7 @@ categories = ["concurrency"] [dev-dependencies] pin-project-lite = "0.2.7" +futures = { version = "0.3" } + +[dependencies] +futures-core = { version = "0.3", default-features = false } \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 37874e1..aa5ec88 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -23,6 +23,8 @@ use core::{ fmt::{self, Debug, Formatter}, pin::Pin, + future::Future, + task::{Context, Poll}, }; /// A mutual exclusion primitive that relies on static type information only @@ -181,3 +183,62 @@ impl From for SyncWrapper { Self::new(value) } } + +/// `Future` which is `Sync`. +/// +/// # Examples +/// +/// ``` +/// use sync_wrapper::{SyncWrapper, SyncFuture}; +/// +/// let fut = async { 1 }; +/// let fut = SyncFuture::new(fut); +/// ``` +pub struct SyncFuture { + inner: SyncWrapper +} +impl SyncFuture { + pub fn new(inner: F) -> Self { + Self { inner: SyncWrapper::new(inner) } + } + pub fn into_inner(self) -> F { + self.inner.into_inner() + } +} +impl Future for SyncFuture { + type Output = F::Output; + fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { + let inner = unsafe { self.map_unchecked_mut(|x| x.inner.get_mut()) }; + inner.poll(cx) + } +} + +/// `Stream` which is `Sync`. +/// +/// # Examples +/// +/// ``` +/// use sync_wrapper::SyncStream; +/// use futures::stream; +/// +/// let st = stream::iter(vec![1]); +/// let st = SyncStream::new(st); +/// ``` +pub struct SyncStream { + inner: SyncWrapper +} +impl SyncStream { + pub fn new(inner: S) -> Self { + Self { inner: SyncWrapper::new(inner) } + } + pub fn into_inner(self) -> S { + self.inner.into_inner() + } +} +impl futures_core::Stream for SyncStream { + type Item = S::Item; + fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { + let inner = unsafe { self.map_unchecked_mut(|x| x.inner.get_mut()) }; + inner.poll_next(cx) + } +} \ No newline at end of file