blob: ef07130493334c072c9520e2081fd72bb1075ccb [file] [log] [blame]
use core::{mem, ptr};
use num::Num;
use std::cmp::min;
pub fn swap_loop<T>(a: &mut [T], b: &mut [T]) {
for (x, y) in a.iter_mut().zip(b.iter_mut()) {
mem::swap(x, y);
}
}
pub fn swap_ptrswap<T>(a: &mut [T], b: &mut [T]) {
unsafe { ptr::swap_nonoverlapping(a.as_mut_ptr(), b.as_mut_ptr(), min(a.len(), b.len())) }
}
/// Exactly equivalent to `swap_2`, but trades the use of unsafe operations against having to know
/// the exact type and size of the vector at compile time. Only works with `heapless::Vec`.
pub fn swap_3<T, N>(a: &mut heapless::Vec<T, N>, b: &mut heapless::Vec<T, N>)
where
N: heapless::ArrayLength<T>,
T: Num + Copy,
{
mem::swap(a, b);
}