blob: ef07130493334c072c9520e2081fd72bb1075ccb [file] [log] [blame]
Matthias Andreas Benkard08586462020-06-28 22:42:43 +02001use core::{mem, ptr};
2use num::Num;
3use std::cmp::min;
4
5pub 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
11pub 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`.
17pub fn swap_3<T, N>(a: &mut heapless::Vec<T, N>, b: &mut heapless::Vec<T, N>)
18where
19 N: heapless::ArrayLength<T>,
20 T: Num + Copy,
21{
22 mem::swap(a, b);
23}