Use Lrc::make_mut instead of Lrc::get_mut

This commit is contained in:
The 8472 2023-07-03 13:40:25 +02:00
parent 14e57d7c13
commit 7916a2c1c9

View File

@ -417,16 +417,10 @@ pub fn map_enumerated_owned(
mut self,
mut f: impl FnMut(usize, TokenTree) -> TokenTree,
) -> TokenStream {
if let Some(inner) = Lrc::get_mut(&mut self.0) {
// optimization: perform the map in-place if self's reference count is 1
let owned = mem::take(inner);
*inner = owned.into_iter().enumerate().map(|(i, tree)| f(i, tree)).collect();
let owned = Lrc::make_mut(&mut self.0); // clone if necessary
// rely on vec's in-place optimizations to avoid another allocation
*owned = mem::take(owned).into_iter().enumerate().map(|(i, tree)| f(i, tree)).collect();
self
} else {
TokenStream(Lrc::new(
self.0.iter().enumerate().map(|(i, tree)| f(i, tree.clone())).collect(),
))
}
}
/// Create a token stream containing a single token with alone spacing.