Introduce RefCell::{try_replace, try_replace_with, try_swap}
#132011
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This adds
Result
-based equivalents toRefCell::replace
,RefCell::replace_with
andRefCell::swap
.This was proposed in #54493, and has not gone through an ACP or RFC process.
Concerns
I believe the design of these functions should be relatively uncontroversial, but their presence at all may be. It's unclear how much these would be used, and they do clutter the documentation of
RefCell
. The main purpose is completeness: offering aResult
-based method for every panickingRefCell
method.Looking at history, RFC-2057 (PR, rendered), which added
replace
andswap
, simply didn't address the possibility oftry_replace
andtry_swap
. The author dismissed them as not very useful.Error types
#54493 proposed adding unique error types for each function. I've opted instead to reuse
BorrowMutError
, matchingtry_borrow_unguarded
reusingBorrowError
.Process?
As I haven't gone through the
rust-lang/rust
PR process much yet, I'd love guidance on what to do with this. I figured writing this PR couldn't hurt, but it's unclear whether an ACP or RFC would be best here.The prior additions of
RefCell::{try_borrow, try_borrow_mut}
andRefCell::{swap, replace}
both used RFCs, but it's unclear to me if things have changed since then.