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