rust-lang / rust-lang/rust

Example for `std::sync::SyncView` is dubious

Open
#146,245 0 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-docs T-libs
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Location (URL)

https://doc.rust-lang.org/nightly/std/sync/struct.SyncView.html#examples

Summary

The relevant portion of the example is:

assert_sync(State {
    future: SyncView::new(async {
        let cell = Cell::new(1);
        let cell_ref = &cell;
        other().await;
        let value = cell_ref.get();
    })
});

While it is true that this code compiles, it isn’t clear that it accomplishes anything useful, because the SyncView-wrapped async block is Sync but not Send, and thus State too is Sync but not Send. Thus, the only benefit of using SyncView here would be

  • if the struct State had other fields which are to be to be accessed from other threads,
  • and that access would have to occur while the future is not being polled.

These aspects are not in the example code nor discussed at all in the example’s accompanying text, and the example briefly misled me into thinking that SyncView could be used to produce a Send future of this sort (in the context of this URLO thread, “Borrow of owned !Sync type in async function”, which contains async {} code essentially identical to this example’s). I think that this example is significantly misleading, and it should ideally be replaced with a different one that illustrates producing Send + Sync from only Send, since Sync without Send is only very narrowly useful. If that is not possible, it should be expanded to at least vaguely gesture at making use of the Sync without Send condition.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the SyncView documentation example at std/sync/struct.SyncView.html#examples and review the shown State and async block alongside the issue's explanation of Send and Sync. Done means replacing or expanding the example and its accompanying text so the practical benefit of Sync without Send is accurately demonstrated and the misleading interpretation is removed.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
documentation
Issue type
Documentation
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.