-
Notifications
You must be signed in to change notification settings - Fork 13.9k
Closed
Labels
B-unstableBlocker: Implemented in the nightly compiler and unstable.Blocker: Implemented in the nightly compiler and unstable.C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCCategory: An issue tracking the progress of sth. like the implementation of an RFCLibs-SmallLibs issues that are considered "small" or self-containedLibs issues that are considered "small" or self-containedLibs-TrackedLibs issues that are tracked on the team's project board.Libs issues that are tracked on the team's project board.T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.Relevant to the library API team, which will review and decide on the PR/issue.disposition-mergeThis issue / PR is in PFCP or FCP with a disposition to merge it.This issue / PR is in PFCP or FCP with a disposition to merge it.finished-final-comment-periodThe final comment period is finished for this PR / Issue.The final comment period is finished for this PR / Issue.

Description
This issue tracks the cell_update
feature.
The feature adds Cell::update
, which allows one to more easily modify the inner value.
For example, instead of c.set(c.get() + 1)
now you can just do c.update(|x| x + 1)
:
let c = Cell::new(5);
let new = c.update(|x| x + 1);
assert_eq!(new, 6);
assert_eq!(c.get(), 6);
stanislav-tkach, jyn514, MaxNanasy, ZOXEXIVO, kaleidawave and 48 moreschneiderfelipe, ldanko and A6GibKm
Metadata
Metadata
Assignees
Labels
B-unstableBlocker: Implemented in the nightly compiler and unstable.Blocker: Implemented in the nightly compiler and unstable.C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCCategory: An issue tracking the progress of sth. like the implementation of an RFCLibs-SmallLibs issues that are considered "small" or self-containedLibs issues that are considered "small" or self-containedLibs-TrackedLibs issues that are tracked on the team's project board.Libs issues that are tracked on the team's project board.T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.Relevant to the library API team, which will review and decide on the PR/issue.disposition-mergeThis issue / PR is in PFCP or FCP with a disposition to merge it.This issue / PR is in PFCP or FCP with a disposition to merge it.finished-final-comment-periodThe final comment period is finished for this PR / Issue.The final comment period is finished for this PR / Issue.
Type
Projects
Milestone
Relationships
Development
Select code repository
Activity
oli-obk commentedon Apr 24, 2018
Not a blocking issue for merging this as an unstable feature, so I'm registering my concern here:
The
update
function returning its new value feels very++i
-ish. Similar to+=
returning the result of the operation (which rust luckily does not!). I don't think we should be doing this. Options I see:()
(that was the suggestion in the PR)&mut T
, and allow the closure to return any value.|x| {*x += 1; *y }
Maybe there are other options?
Data point: the
volatile
crate'supdate
method returns()
(https://github.com/embed-rs/volatile/blob/master/src/lib.rs#L134)ghost commentedon Apr 24, 2018
@oli-obk
I like the second option (taking a
&mut T
in the closure). There's an interesting variation on this option - instead of requiringT: Copy
we could requireT: Default
to support updates like this one:Perhaps we could have two methods with different names that clearly reflect how they internally work?
ghost commentedon May 2, 2018
What do you think, @SimonSapin? Would two such methods,
get_update
andtake_update
, be a more appropriate interface?&mut T
to&Cell<T>
#50494SimonSapin commentedon May 14, 2018
The
T: Default
flavor feels kinda awkward but I don’t know how to put that in more concrete terms, sorry :/It looks like there is a number of slightly different possible APIs for this. I don’t have a strong opinion on which is (or are) preferable.
Auto merge of #50494 - F001:as_cell, r=alexcrichton
CodeSandwich commentedon Aug 6, 2018
Taking
&mut
in this method would be awesome, I would love to use it.Unfortunately it seems that it would have to be
unsafe
, because AFAIK there is no way in Rust to restrict usage like this:I hope, I'm wrong.
SimonSapin commentedon Aug 7, 2018
@CodeSandwich it is a fundamental restriction of
Cell<T>
that a reference&T
or&mut T
(or anything else inside ofT
) must not be given to the value inside the cell. It’s what makes it sound. (This is the difference withRefCell
.)We could make an API that takes a closure that takes
&mut T
, but that reference would not be to the inside of the cell. It would be to value that has been copied (withT: Copy
) or moved (and temporarily replaced with some other value, withT: Default
) onto the stack and will be copied/moved back when the closure returns. So code like your example would be arguably buggy but still sound: the innerupdate()
would operate on a temporary value that would be overwritten at the end of the outer one.CodeSandwich commentedon Aug 13, 2018
I think, that a modifier method might be safe if we just forbid usage of reference to
Cell
'sself
inside. Rust does not have mechanism for forbidding usage of a SPECIFIC reference, but it does have mechanism for forbidding usage of ALL references:'static
closures.I've created a simple implementation of such method and I couldn't find any hole in it. It's a limited solution, but it's enough for many use cases including simple number and collections modifications.
SimonSapin commentedon Aug 13, 2018
A
'static
closure could still reach theCell
, for example by owning aRc
.CodeSandwich commentedon Aug 13, 2018
That's absolutely right :(
86 remaining items
tgross35 commentedon Apr 2, 2025
I can't update the top post because it was created by a deleted user, but the API will be:
Rollup merge of rust-lang#139273 - tgross35:cell-update-changes, r=jh…
Rollup merge of rust-lang#139273 - tgross35:cell-update-changes, r=jh…
Unrolled build for rust-lang#139273
Stabilize the `cell_update` feature
tgross35 commentedon Apr 7, 2025
Crosslinking, FCP for the changed API is currently ongoing at #134446 (comment)
Apply requested API changes to `cell_update`
Rollup merge of rust-lang#139273 - tgross35:cell-update-changes, r=jh…
Rollup merge of rust-lang#134446 - tgross35:stabilize-cell_update, r=…
Unrolled build for rust-lang#134446
Stabilize the `cell_update` feature
Rollup merge of #134446 - tgross35:stabilize-cell_update, r=jhpratt
Stabilize the `cell_update` feature
Rollup merge of rust-lang#134446 - tgross35:stabilize-cell_update, r=…