Github Don't implement mem::replace with mem::swap. by m-ou-se · Pull Request #8...
source link: https://github.com/rust-lang/rust/pull/83022
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.
swap
is a complicated operation, so this changes the implementation of replace
to use read
and write
instead.
See #83019.
I wrote there:
Implementing the simpler operation (replace) with the much more complicated operation (swap) doesn't make a whole lot of sense.
replace
is just read+write, and the primitive for moving out of a&mut
.swap
is for doing that to two&mut
at the same time, which is both more niche and more complicated (as shown byswap_nonoverlapping_bytes
).
This could be especially interesting for Option<VeryLargeStruct>::take()
, since swapping such a large structure with swap_nonoverlapping_bytes
is going to be much less efficient than ptr::write()
'ing a None
.
But also for small values where swap
just reads/writes using temporary variable, this makes a replace
or take
operation simpler:
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK