impl `Arc::unwrap_or_clone` by derekdreery · Pull Request #91589 · rust-lang/rus...
source link: https://github.com/rust-lang/rust/pull/91589
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
This seems reasonable to me, though I wonder if we should (also) have a more generic unwrap_or(|..| ..)
function. (Not sure what the exact signature should be though.) The existing try_unwrap
function comes close, but it doesn't reduce the reference count if ownership wasn't unique, leaving that to the point where the Err(Arc)
gets dropped afterwards, resulting in more operations on the atomic counter than necessary. unwrap_or_clone
or a more generic unwrap_or(..)
could always decrement the counter unconditionally, since they don't return the Arc
in any case. (Edit: Not true, see comment below. Only true for unwrap() -> Option<T>
.)
For this pr:
- Please also add the same function to
Rc
, to keepArc
andRc
in sync. - You can optimize the
Arc
implementation by using a singlefetch_sub
operation. Check theDrop
implementation forArc
for what that looks like. (And check thetry_unwrap
implementation for how to take theT
out of theArc
and cleaning it up.) - Please open a tracking issue for the issue number in the
#[unstable]
attributes.
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK