5

impl `Arc::unwrap_or_clone` by derekdreery · Pull Request #91589 · rust-lang/rus...

 2 years ago
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.

Copy link

Member

m-ou-se commented 15 days ago

edited

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 keep Arc and Rc in sync.
  • You can optimize the Arc implementation by using a single fetch_sub operation. Check the Drop implementation for Arc for what that looks like. (And check the try_unwrap implementation for how to take the T out of the Arc and cleaning it up.)
  • Please open a tracking issue for the issue number in the #[unstable] attributes.

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK