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