Skip to content

Commit 5262752

Browse files
authored
Rollup merge of #110633 - scottmcm:more-take, r=thomcc
More `mem::take` in `library` A bunch of places were using `replace(…, &mut [])`, but that can just be `take`.
2 parents da7c7d6 + b4f4388 commit 5262752

File tree

5 files changed

+10
-10
lines changed

5 files changed

+10
-10
lines changed

alloc/src/vec/drain.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ impl<T, A: Allocator> Drop for Drain<'_, T, A> {
197197
}
198198
}
199199

200-
let iter = mem::replace(&mut self.iter, (&mut []).iter());
200+
let iter = mem::take(&mut self.iter);
201201
let drop_len = iter.len();
202202

203203
let mut vec = self.vec;

core/src/slice/iter.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -685,7 +685,7 @@ where
685685
None
686686
} else {
687687
self.finished = true;
688-
Some(mem::replace(&mut self.v, &mut []))
688+
Some(mem::take(&mut self.v))
689689
}
690690
}
691691
}
@@ -749,7 +749,7 @@ where
749749
match idx_opt {
750750
None => self.finish(),
751751
Some(idx) => {
752-
let tmp = mem::replace(&mut self.v, &mut []);
752+
let tmp = mem::take(&mut self.v);
753753
let (head, tail) = tmp.split_at_mut(idx);
754754
self.v = head;
755755
Some(&mut tail[1..])
@@ -830,7 +830,7 @@ where
830830
if idx == self.v.len() {
831831
self.finished = true;
832832
}
833-
let tmp = mem::replace(&mut self.v, &mut []);
833+
let tmp = mem::take(&mut self.v);
834834
let (head, tail) = tmp.split_at_mut(idx);
835835
self.v = tail;
836836
Some(head)
@@ -876,7 +876,7 @@ where
876876
if idx == 0 {
877877
self.finished = true;
878878
}
879-
let tmp = mem::replace(&mut self.v, &mut []);
879+
let tmp = mem::take(&mut self.v);
880880
let (head, tail) = tmp.split_at_mut(idx);
881881
self.v = head;
882882
Some(tail)

std/src/io/impls.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ impl Write for &mut [u8] {
348348
#[inline]
349349
fn write(&mut self, data: &[u8]) -> io::Result<usize> {
350350
let amt = cmp::min(data.len(), self.len());
351-
let (a, b) = mem::replace(self, &mut []).split_at_mut(amt);
351+
let (a, b) = mem::take(self).split_at_mut(amt);
352352
a.copy_from_slice(&data[..amt]);
353353
*self = b;
354354
Ok(amt)

std/src/io/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ mod tests;
253253

254254
use crate::cmp;
255255
use crate::fmt;
256-
use crate::mem::replace;
256+
use crate::mem::take;
257257
use crate::ops::{Deref, DerefMut};
258258
use crate::slice;
259259
use crate::str;
@@ -1186,7 +1186,7 @@ impl<'a> IoSliceMut<'a> {
11861186
}
11871187
}
11881188

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

1332-
*bufs = &mut replace(bufs, &mut [])[remove..];
1332+
*bufs = &mut take(bufs)[remove..];
13331333
if bufs.is_empty() {
13341334
assert!(n == accumulated_len, "advancing io slices beyond their length");
13351335
} else {

std/src/sys/unsupported/io.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ impl<'a> IoSliceMut<'a> {
3030

3131
#[inline]
3232
pub fn advance(&mut self, n: usize) {
33-
let slice = mem::replace(&mut self.0, &mut []);
33+
let slice = mem::take(&mut self.0);
3434
let (_, remaining) = slice.split_at_mut(n);
3535
self.0 = remaining;
3636
}

0 commit comments

Comments
 (0)