Add Atomic*::from_mut_slice by cuviper · Pull Request #94384 · rust-lang/rust (original) (raw)

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 andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Conversation10 Commits1 Checks0 Files changed

Conversation

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters

[ Show hidden characters]({{ revealButtonHref }})

@cuviper

Tracking issue #76314 for from_mut has a question about the possibility of from_mut_slice, and I found a real case for it. A user in the forum had a parallelism problem that could be solved by open-indexing updates to a vector of atomics, but they didn't want to affect the other code using that vector. Using from_mut_slice, they could borrow that data as atomics just long enough for their parallel loop.

ref: https://users.rust-lang.org/t/sharing-vector-with-rayon-par-iter-correctly/72022

@cuviper

@rust-highfive

r? @Mark-Simulacrum

(rust-highfive has picked a reviewer for you, use r? to override)

@Mark-Simulacrum

@cuviper

it seems like if you have stateless &mut T -> &mut U, you can do &mut [T] -> &mut [U], right?

Yes, the constraint is almost like T: AsMut<U>, but they have to be the same size and return the same address, no dereferencing. It's similar to coercion too. Maybe something like unsafe trait CoerceMut<T> with a blanket impl for slices?

dtolnay

Choose a reason for hiding this comment

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

Nice!

@dtolnay

@bors

📌 Commit d3d2a27 has been approved by dtolnay

@bors bors added S-waiting-on-bors

Status: Waiting on bors to run and complete tests. Bors will change the label on completion.

and removed S-waiting-on-review

Status: Awaiting review from the assignee but also interested parties.

labels

Feb 28, 2022

bors added a commit to rust-lang-ci/rust that referenced this pull request

Mar 1, 2022

@bors

Rollup of 7 pull requests

Successful merges:

Failed merges:

r? @ghost @rustbot modify labels: rollup

@m-ou-se

:D Coincidentally I just got to my to do list item that says "Add atomic from_mut_slice" and noticed you just added it a few days ago. Nice!

@WaffleLapkin

Should we also introduce the inverse operation? i.e. &mut [Atomic*] -> &mut [*]?

just like Atomic*::from_mut has an inverse Atomic*::get_mut.

@cuviper

@WaffleLapkin that's a bit awkward because I don't think we can have a &mut [self] method. So it would either have to be a method on actual slice impls, or else an explicit associated method like Atomic*::get_mut_slice(this: &mut [Self]).

@WaffleLapkin

For me an explicit associated method seems fine. Can make a PR 👀

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request

Mar 13, 2022

@matthiaskrgr

…r=Mark-Simulacrum

Add Atomic*::get_mut_slice

This PR adds the inverse of Atomic*::from_mut_slice introduced in rust-lang#94384 with the following API:

// core::sync::atomic

impl Atomic* {
    fn get_mut_slice(this: &mut [Self]) -> &mut [*];
}

cc @cuviper


For now I've used the same tracking issue as Atomic*::from_mut_slice, should I open a new one?

Labels

S-waiting-on-bors

Status: Waiting on bors to run and complete tests. Bors will change the label on completion.