Skip to content
Merged
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
73 changes: 43 additions & 30 deletions library/core/src/sync/sync_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,49 +26,62 @@ use core::task::{Context, Poll};
///
/// ## Examples
///
/// Using a non-`Sync` future prevents the wrapping struct from being `Sync`:
/// A non-`Sync` field prevents the wrapping struct from being `Sync`:
///
/// ```compile_fail
/// use core::cell::Cell;
/// ```compile_fail,E0277
/// use std::sync::mpsc::{self, Receiver};
///
/// async fn other() {}
/// fn assert_sync<T: Sync>(t: T) {}
/// struct State<F> {
/// future: F
/// struct Inbox {
/// name: &'static str,
/// receiver: Receiver<u32>,
/// }
///
/// assert_sync(State {
/// future: async {
/// let cell = Cell::new(1);
/// let cell_ref = &cell;
/// other().await;
/// let value = cell_ref.get();
/// }
/// });
/// fn require_send<T: Send>() {}
/// fn require_send_sync<T: Send + Sync>() {}
///
/// require_send::<Inbox>(); // compiled
/// require_send_sync::<Inbox>(); // compile-failed
/// ```
///
/// `SyncView` ensures the struct is `Sync` without stripping the future of its
/// `SyncView` makes the value `Sync` without stripping the struct of its
/// functionality:
///
/// ```
/// ```ignore-wasm
Comment on lines -53 to +49

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This gives a "This example is not tested on wasm" warning in the docs https://doc.rust-lang.org/nightly/std/sync/struct.SyncView.html#examples which is a bit weird. Perhaps this and the original Atomic example should gate on target_has_threads instead?

This isn't super accurate anyway since we have wasm targets with threads and non-wasm targets without threads, it just happens that the wasm target in CI is the only thing we flag.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your suggestion.

My concern is that target_has_threads doesn't guarantee that std::thread::spawn is supported.

For example, on wasm32-unknown-unknown, enabling +atomics makes singlethread() return false, which enables target_has_threads. However, std still selects the unsupported thread backend, where Thread::new always returns UNSUPPORTED_PLATFORM.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good point too. Opened a discussion at #t-libs > Skipping doctests without thread::spawn

/// #![feature(exclusive_wrapper)]
/// use core::cell::Cell;
/// use core::sync::SyncView;
///
/// async fn other() {}
/// fn assert_sync<T: Sync>(t: T) {}
/// struct State<F> {
/// future: SyncView<F>
/// use std::sync::SyncView;
/// use std::sync::mpsc::{self, Receiver};
/// use std::thread;
///
/// struct Inbox {
/// name: &'static str,
/// receiver: SyncView<Receiver<u32>>,
/// }
///
/// impl Inbox {
/// fn name(&self) -> &'static str {
/// self.name
/// }
///
/// fn recv(&mut self) -> u32 {
/// self.receiver.as_mut().recv().unwrap()
/// }
/// }
///
/// assert_sync(State {
/// future: SyncView::new(async {
/// let cell = Cell::new(1);
/// let cell_ref = &cell;
/// other().await;
/// let value = cell_ref.get();
/// })
/// let (sender, receiver) = mpsc::channel();
/// let mut inbox = Inbox { name: "jobs", receiver: SyncView::new(receiver) };
/// sender.send(42).unwrap();
/// drop(sender);
///
/// thread::scope(|scope| {
/// let reader = scope.spawn(|| inbox.name());
/// assert_eq!(inbox.name(), "jobs");
/// assert_eq!(reader.join().unwrap(), "jobs");
/// });
///
/// let message = thread::spawn(move || inbox.recv()).join().unwrap();
/// assert_eq!(message, 42);
/// println!("Shared Inbox across threads, then moved it to a worker and received 42");
/// ```
///
/// ## Parallels with a mutex
Expand Down
Loading