Matthias Andreas Benkard | 0858646 | 2020-06-28 22:42:43 +0200 | [diff] [blame] | 1 | use core::{mem, ptr}; |
| 2 | use num::Num; |
| 3 | use std::cmp::min; |
| 4 | |
| 5 | pub fn swap_loop<T>(a: &mut [T], b: &mut [T]) { |
| 6 | for (x, y) in a.iter_mut().zip(b.iter_mut()) { |
| 7 | mem::swap(x, y); |
| 8 | } |
| 9 | } |
| 10 | |
| 11 | pub fn swap_ptrswap<T>(a: &mut [T], b: &mut [T]) { |
| 12 | unsafe { ptr::swap_nonoverlapping(a.as_mut_ptr(), b.as_mut_ptr(), min(a.len(), b.len())) } |
| 13 | } |
| 14 | |
| 15 | /// Exactly equivalent to `swap_2`, but trades the use of unsafe operations against having to know |
| 16 | /// the exact type and size of the vector at compile time. Only works with `heapless::Vec`. |
| 17 | pub fn swap_3<T, N>(a: &mut heapless::Vec<T, N>, b: &mut heapless::Vec<T, N>) |
| 18 | where |
| 19 | N: heapless::ArrayLength<T>, |
| 20 | T: Num + Copy, |
| 21 | { |
| 22 | mem::swap(a, b); |
| 23 | } |