Matthias Andreas Benkard | 6219ab8 | 2020-06-29 19:42:29 +0200 | [diff] [blame] | 1 | use crate::bindings; |
| 2 | use crate::swapper::Swapper; |
| 3 | use std::cmp::min; |
| 4 | use std::mem::size_of; |
| 5 | |
| 6 | pub struct CLoopSwapper {} |
| 7 | impl Swapper for CLoopSwapper { |
| 8 | fn swap<T>(a: &mut [T], b: &mut [T]) { |
| 9 | unsafe { |
| 10 | bindings::swap_loop( |
| 11 | a.as_mut_ptr() as *mut i8, |
| 12 | b.as_mut_ptr() as *mut i8, |
| 13 | (size_of::<T>() * min(a.len(), b.len())) as bindings::size_t, |
| 14 | ) |
| 15 | } |
| 16 | } |
| 17 | } |
| 18 | |
| 19 | pub struct CMallocSwapper {} |
| 20 | impl Swapper for CMallocSwapper { |
| 21 | fn swap<T>(a: &mut [T], b: &mut [T]) { |
| 22 | unsafe { |
| 23 | bindings::swap_malloc( |
| 24 | a.as_mut_ptr() as *mut i8, |
| 25 | b.as_mut_ptr() as *mut i8, |
| 26 | (size_of::<T>() * min(a.len(), b.len())) as bindings::size_t, |
| 27 | ) |
| 28 | } |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | pub struct CAllocaSwapper {} |
| 33 | impl Swapper for CAllocaSwapper { |
| 34 | fn swap<T>(a: &mut [T], b: &mut [T]) { |
| 35 | unsafe { |
| 36 | bindings::swap_alloca( |
| 37 | a.as_mut_ptr() as *mut i8, |
| 38 | b.as_mut_ptr() as *mut i8, |
| 39 | (size_of::<T>() * min(a.len(), b.len())) as bindings::size_t, |
| 40 | ) |
| 41 | } |
| 42 | } |
| 43 | } |