blob: a9d7733c1dc61ff292d9018ca13bec9a5e1f74cf [file] [log] [blame]
Matthias Andreas Benkard6219ab82020-06-29 19:42:29 +02001use crate::bindings;
2use crate::swapper::Swapper;
3use std::cmp::min;
4use std::mem::size_of;
5
6pub struct CLoopSwapper {}
7impl 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
19pub struct CMallocSwapper {}
20impl 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
32pub struct CAllocaSwapper {}
33impl 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}