Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add default wrapper implementations #1

Merged
merged 2 commits into from
Aug 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
61 changes: 61 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -181,3 +183,62 @@ impl<T> From<T> for SyncWrapper<T> {
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<F> {
inner: SyncWrapper<F>
}
impl <F: Future> SyncFuture<F> {
pub fn new(inner: F) -> Self {
Self { inner: SyncWrapper::new(inner) }
}
pub fn into_inner(self) -> F {
self.inner.into_inner()
}
}
impl <F: Future> Future for SyncFuture<F> {
type Output = F::Output;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
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<S> {
inner: SyncWrapper<S>
}
impl <S: futures_core::Stream> SyncStream<S> {
pub fn new(inner: S) -> Self {
Self { inner: SyncWrapper::new(inner) }
}
pub fn into_inner(self) -> S {
self.inner.into_inner()
}
}
impl <S: futures_core::Stream> futures_core::Stream for SyncStream<S> {
type Item = S::Item;
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
let inner = unsafe { self.map_unchecked_mut(|x| x.inner.get_mut()) };
inner.poll_next(cx)
}
}