Matthias Andreas Benkard | 6219ab8 | 2020-06-29 19:42:29 +0200 | [diff] [blame] | 1 | use crate::swapper::Swapper; |
| 2 | |
| 3 | use core::{mem, ptr}; |
| 4 | use std::cmp::min; |
| 5 | |
| 6 | pub struct LoopSwapper {} |
| 7 | impl Swapper for LoopSwapper { |
| 8 | fn swap<T>(a: &mut [T], b: &mut [T]) { |
| 9 | for (x, y) in a.iter_mut().zip(b.iter_mut()) { |
| 10 | mem::swap(x, y); |
| 11 | } |
| 12 | } |
| 13 | } |
| 14 | |
| 15 | pub struct PtrSwapper {} |
| 16 | impl Swapper for PtrSwapper { |
| 17 | fn swap<T>(a: &mut [T], b: &mut [T]) { |
| 18 | unsafe { ptr::swap_nonoverlapping(a.as_mut_ptr(), b.as_mut_ptr(), min(a.len(), b.len())) } |
| 19 | } |
| 20 | } |