tracers_core/argtypes/
int.rs

1use super::{ProbeArgType, ProbeArgWrapper};
2
3#[cfg(test)]
4extern crate quickcheck;
5
6// Using the macro to avoid duplication, implement ProbeArgType and ProbeArgWrapper for the
7// intrinsic integer types, with a few non-obvious translations based on C variadic idiosyncracies
8macro_rules! impl_integer_arg_type {
9    ( $rust_type:ty, $c_type:ty, $tests:ident ) => {
10        impl ProbeArgType<$rust_type> for $rust_type {
11            type WrapperType = $rust_type;
12
13            fn wrap(arg: $rust_type) -> Self::WrapperType {
14                arg
15            }
16        }
17
18        impl ProbeArgWrapper for $rust_type {
19            type CType = $c_type;
20
21            fn as_c_type(&self) -> Self::CType {
22                //*self as $c_type
23                <$c_type>::from(*self)
24            }
25        }
26
27        #[cfg(test)]
28        mod $tests {
29            use crate::{wrap, ProbeArgWrapper};
30            use std::mem::size_of;
31
32            #[quickcheck]
33            fn converts_to_c_type(x: $rust_type) {
34                let wrapper = wrap(x);
35
36                assert_eq!(size_of::<$rust_type>(), size_of::<$c_type>());
37                assert_eq!(<$c_type>::from(x), wrapper.as_c_type());
38            }
39        }
40    };
41}
42
43impl_integer_arg_type!(usize, libc::size_t, usize_test);
44impl_integer_arg_type!(isize, libc::ssize_t, isize_test);
45impl_integer_arg_type!(u64, std::os::raw::c_ulonglong, u64_test);
46impl_integer_arg_type!(i64, std::os::raw::c_longlong, i64_test);
47impl_integer_arg_type!(u32, std::os::raw::c_uint, u32_test);
48impl_integer_arg_type!(i32, std::os::raw::c_int, i32_test);
49impl_integer_arg_type!(u16, std::os::raw::c_ushort, u16_test);
50impl_integer_arg_type!(i16, std::os::raw::c_short, i16_test);
51impl_integer_arg_type!(u8, std::os::raw::c_uchar, u8_test);
52impl_integer_arg_type!(i8, std::os::raw::c_char, i8_test);