gcrypt/
utils.rs

1macro_rules! impl_wrapper {
2    ($Name:ident: $T:ty) => {
3        #[inline]
4        pub unsafe fn from_raw(raw: $T) -> Self {
5            $Name(NonNull::<$T>::new(raw).unwrap())
6        }
7
8        #[inline]
9        pub fn as_raw(&self) -> $T {
10            self.0.as_ptr()
11        }
12
13        #[inline]
14        pub fn into_raw(self) -> $T {
15            let raw = self.as_raw();
16            ::std::mem::forget(self);
17            raw
18        }
19    };
20}
21
22macro_rules! ffi_enum_wrapper {
23    ($(#[$Attr:meta])* pub enum $Name:ident: $T:ty {
24        $($(#[$ItemAttr:meta])* $Item:ident = $Value:expr),+
25    }) => {
26        #[derive(Copy, Clone, Eq, PartialEq, Hash)]
27        $(#[$Attr])*
28        pub enum $Name {
29            $($(#[$ItemAttr])* $Item,)+
30            Other($T),
31        }
32
33        impl $Name {
34            #[inline]
35            pub unsafe fn from_raw(raw: $T) -> $Name {
36                $(if raw == ($Value as $T) {
37                    $Name::$Item
38                } else )+ {
39                    $Name::Other(raw)
40                }
41            }
42
43            #[inline]
44            pub fn raw(&self) -> $T {
45                match *self {
46                    $($Name::$Item => $Value as $T,)+
47                    $Name::Other(other) => other,
48                }
49            }
50        }
51
52        impl ::std::fmt::Debug for $Name {
53            fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
54                match *self {
55                    $($Name::$Item => {
56                        write!(f, concat!(stringify!($Name), "::",
57                                          stringify!($Item), "({:?})"), self.raw())
58                    })+
59                    _ => write!(f, concat!(stringify!($Name), "({:?})"), self.raw()),
60                }
61            }
62        }
63    };
64    ($(#[$Attr:meta])* pub enum $Name:ident: $T:ty {
65        $($(#[$ItemAttr:meta])* $Item:ident = $Value:expr,)+
66    }) => {
67        ffi_enum_wrapper! {
68            $(#[$Attr])*
69            pub enum $Name: $T {
70                $($(#[$ItemAttr])* $Item = $Value),+
71            }
72        }
73    };
74}
75
76pub(crate) trait Ptr {
77    type Inner;
78}
79
80impl<T> Ptr for *mut T {
81    type Inner = T;
82}
83
84impl<T> Ptr for *const T {
85    type Inner = T;
86}