Skip to content

Make AsyncStream Sync even if the inner future is not #115

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
5 changes: 3 additions & 2 deletions async-stream/src/async_stream.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::sync_wrapper::SyncWrapper;
use crate::yielder::Receiver;

use futures_core::{FusedStream, Stream};
Expand All @@ -13,7 +14,7 @@ pin_project! {
rx: Receiver<T>,
done: bool,
#[pin]
generator: U,
generator: SyncWrapper<U>,
}
}

Expand All @@ -23,7 +24,7 @@ impl<T, U> AsyncStream<T, U> {
AsyncStream {
rx,
done: false,
generator,
generator: SyncWrapper::new(generator),
}
}
}
Expand Down
1 change: 1 addition & 0 deletions async-stream/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@

mod async_stream;
mod next;
mod sync_wrapper;
mod yielder;

/// Asynchronous stream
Expand Down
51 changes: 51 additions & 0 deletions async-stream/src/sync_wrapper.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
use std::fmt;
use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll};

/// A wrapper around `T` that only allows mutable access.
///
/// This allows it to unconditionally implement `Sync`, since there is nothing
/// you can do with an `&SyncWrapper<T>`.
pub(crate) struct SyncWrapper<T> {
inner: T,
}

impl<T> SyncWrapper<T> {
pub(crate) fn new(value: T) -> Self {
Self { inner: value }
}

pub(crate) fn get_pinned_mut(self: Pin<&mut Self>) -> Pin<&mut T> {
// We can't use pin_project! for this because it generates a project_ref
// method which would allow accessing the inner element
//
// SAFETY: this.inner is guaranteed not to move as long as this lives.
unsafe { self.map_unchecked_mut(|this| &mut this.inner) }
}
}

// SAFETY: It is not possible to do anything with an &SyncWrapper<T> so it is
// safe for it to be shared between threads.
//
// See [0] for more details.
//
// [0]: https://internals.rust-lang.org/t/what-shall-sync-mean-across-an-await/12020/2
unsafe impl<T> Sync for SyncWrapper<T> {}

impl<T: Future> Future for SyncWrapper<T> {
type Output = T::Output;

fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
self.get_pinned_mut().poll(cx)
}
}

impl<T> fmt::Debug for SyncWrapper<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
// We can't format the inner value (since that would create an &T reference)
// so we just print a placeholder string.

f.write_str("<opaque future>")
}
}
14 changes: 14 additions & 0 deletions async-stream/tests/stream.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::cell::Cell;

use async_stream::stream;

use futures_core::stream::{FusedStream, Stream};
Expand Down Expand Up @@ -229,6 +231,18 @@ fn inner_try_stream() {
};
}

#[test]
fn stream_is_sync() {
fn assert_sync<T: Sync>(_: T) {}

// The stream should be sync even if it contains a non-sync value.
assert_sync(stream! {
let cell = Cell::new(true);
yield 5;
drop(cell);
});
}

#[rustversion::attr(not(stable), ignore)]
#[test]
fn test() {
Expand Down
Loading