objc_encode/
encode.rs
1use core::ffi::c_void;
2
3use crate::Encoding;
4
5pub unsafe trait Encode {
11 const ENCODING: Encoding<'static>;
13}
14
15macro_rules! encode_impls {
16 ($($t:ty : $e:ident,)*) => ($(
17 unsafe impl Encode for $t {
18 const ENCODING: Encoding<'static> = Encoding::$e;
19 }
20 )*);
21}
22
23encode_impls!(
24 i8: Char,
25 i16: Short,
26 i32: Int,
27 i64: LongLong,
28 u8: UChar,
29 u16: UShort,
30 u32: UInt,
31 u64: ULongLong,
32 f32: Float,
33 f64: Double,
34 bool: Bool,
35 (): Void,
36 *mut i8: String,
37 *const i8: String,
38 *mut u8: String,
39 *const u8: String,
40);
41
42unsafe impl Encode for isize {
43 #[cfg(target_pointer_width = "32")]
44 const ENCODING: Encoding<'static> = i32::ENCODING;
45
46 #[cfg(target_pointer_width = "64")]
47 const ENCODING: Encoding<'static> = i64::ENCODING;
48}
49
50unsafe impl Encode for usize {
51 #[cfg(target_pointer_width = "32")]
52 const ENCODING: Encoding<'static> = u32::ENCODING;
53
54 #[cfg(target_pointer_width = "64")]
55 const ENCODING: Encoding<'static> = u64::ENCODING;
56}
57
58unsafe impl Encode for *mut c_void {
59 const ENCODING: Encoding<'static> = Encoding::Pointer(&Encoding::Void);
60}
61
62unsafe impl Encode for *const c_void {
63 const ENCODING: Encoding<'static> = Encoding::Pointer(&Encoding::Void);
64}
65
66macro_rules! slice_encode_impl {
68 ($($n:literal),* $(,)?) => {
69 $(
70 unsafe impl<T: Encode> Encode for [T; $n] {
71 const ENCODING: Encoding<'static> = Encoding::Array($n, &<T as Encode>::ENCODING);
72 }
73 )*
74 };
75}
76
77slice_encode_impl!(
78 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,
79 27, 28, 29, 30, 31, 32
80);
81
82unsafe impl<T: 'static> Encode for *const T where for<'a> &'a T: Encode {
90 const ENCODING: Encoding<'static> = <&T as Encode>::ENCODING;
91}
92
93unsafe impl<T: 'static> Encode for *mut T where for<'a> &'a mut T: Encode {
94 const ENCODING: Encoding<'static> = <&mut T>::ENCODING;
95}
96
97unsafe impl<'a, T: 'a> Encode for Option<&'a T> where &'a T: Encode {
98 const ENCODING: Encoding<'static> = <&T>::ENCODING;
99}
100
101unsafe impl<'a, T: 'a> Encode for Option<&'a mut T> where &'a mut T: Encode {
102 const ENCODING: Encoding<'static> = <&mut T>::ENCODING;
103}