Skip to content

Commit 85049e5

Browse files
committed
avoid recursion
1 parent 4bcfbc3 commit 85049e5

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

src/libcore/mem.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -450,11 +450,14 @@ pub fn swap<T>(x: &mut T, y: &mut T) {
450450
let x = x as *mut T as *mut u8;
451451
let y = y as *mut T as *mut u8;
452452

453-
// use an xor-swap as x & y are guaranteed to never alias
454-
for i in 0..size_of::<T>() as isize {
453+
// can't use a for loop as the `range` impl calls `mem::swap` recursively
454+
let mut i = 0;
455+
while i < size_of::<T>() as isize {
456+
// use an xor-swap as x & y are guaranteed to never alias
455457
*x.offset(i) ^= *y.offset(i);
456458
*y.offset(i) ^= *x.offset(i);
457459
*x.offset(i) ^= *y.offset(i);
460+
i += 1;
458461
}
459462
}
460463
}

0 commit comments

Comments
 (0)