Skip to content

Commit

Permalink
More mem::take in library
Browse files Browse the repository at this point in the history
A bunch of places were using `replace(…, &mut [])`, but that can just be `take`.
  • Loading branch information
scottmcm committed Apr 21, 2023
1 parent d19b64f commit 8055bb8
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 10 deletions.
2 changes: 1 addition & 1 deletion library/alloc/src/vec/drain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ impl<T, A: Allocator> Drop for Drain<'_, T, A> {
}
}

let iter = mem::replace(&mut self.iter, (&mut []).iter());
let iter = mem::take(&mut self.iter);
let drop_len = iter.len();

let mut vec = self.vec;
Expand Down
8 changes: 4 additions & 4 deletions library/core/src/slice/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,7 @@ where
None
} else {
self.finished = true;
Some(mem::replace(&mut self.v, &mut []))
Some(mem::take(&mut self.v))
}
}
}
Expand Down Expand Up @@ -749,7 +749,7 @@ where
match idx_opt {
None => self.finish(),
Some(idx) => {
let tmp = mem::replace(&mut self.v, &mut []);
let tmp = mem::take(&mut self.v);
let (head, tail) = tmp.split_at_mut(idx);
self.v = head;
Some(&mut tail[1..])
Expand Down Expand Up @@ -830,7 +830,7 @@ where
if idx == self.v.len() {
self.finished = true;
}
let tmp = mem::replace(&mut self.v, &mut []);
let tmp = mem::take(&mut self.v);
let (head, tail) = tmp.split_at_mut(idx);
self.v = tail;
Some(head)
Expand Down Expand Up @@ -876,7 +876,7 @@ where
if idx == 0 {
self.finished = true;
}
let tmp = mem::replace(&mut self.v, &mut []);
let tmp = mem::take(&mut self.v);
let (head, tail) = tmp.split_at_mut(idx);
self.v = head;
Some(tail)
Expand Down
2 changes: 1 addition & 1 deletion library/std/src/io/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ impl Write for &mut [u8] {
#[inline]
fn write(&mut self, data: &[u8]) -> io::Result<usize> {
let amt = cmp::min(data.len(), self.len());
let (a, b) = mem::replace(self, &mut []).split_at_mut(amt);
let (a, b) = mem::take(self).split_at_mut(amt);
a.copy_from_slice(&data[..amt]);
*self = b;
Ok(amt)
Expand Down
6 changes: 3 additions & 3 deletions library/std/src/io/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ mod tests;

use crate::cmp;
use crate::fmt;
use crate::mem::replace;
use crate::mem::take;
use crate::ops::{Deref, DerefMut};
use crate::slice;
use crate::str;
Expand Down Expand Up @@ -1186,7 +1186,7 @@ impl<'a> IoSliceMut<'a> {
}
}

*bufs = &mut replace(bufs, &mut [])[remove..];
*bufs = &mut take(bufs)[remove..];
if bufs.is_empty() {
assert!(n == accumulated_len, "advancing io slices beyond their length");
} else {
Expand Down Expand Up @@ -1329,7 +1329,7 @@ impl<'a> IoSlice<'a> {
}
}

*bufs = &mut replace(bufs, &mut [])[remove..];
*bufs = &mut take(bufs)[remove..];
if bufs.is_empty() {
assert!(n == accumulated_len, "advancing io slices beyond their length");
} else {
Expand Down
2 changes: 1 addition & 1 deletion library/std/src/sys/unsupported/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl<'a> IoSliceMut<'a> {

#[inline]
pub fn advance(&mut self, n: usize) {
let slice = mem::replace(&mut self.0, &mut []);
let slice = mem::take(&mut self.0);
let (_, remaining) = slice.split_at_mut(n);
self.0 = remaining;
}
Expand Down

0 comments on commit 8055bb8

Please sign in to comment.