Refactor swapper code with traits.
Change-Id: Ia27d3a11fea86d2ac430c3455d5247cfc4891e70
diff --git a/src/swapper/rust_swap.rs b/src/swapper/rust_swap.rs
new file mode 100644
index 0000000..a11f303
--- /dev/null
+++ b/src/swapper/rust_swap.rs
@@ -0,0 +1,20 @@
+use crate::swapper::Swapper;
+
+use core::{mem, ptr};
+use std::cmp::min;
+
+pub struct LoopSwapper {}
+impl Swapper for LoopSwapper {
+ fn swap<T>(a: &mut [T], b: &mut [T]) {
+ for (x, y) in a.iter_mut().zip(b.iter_mut()) {
+ mem::swap(x, y);
+ }
+ }
+}
+
+pub struct PtrSwapper {}
+impl Swapper for PtrSwapper {
+ fn swap<T>(a: &mut [T], b: &mut [T]) {
+ unsafe { ptr::swap_nonoverlapping(a.as_mut_ptr(), b.as_mut_ptr(), min(a.len(), b.len())) }
+ }
+}