|
| 1 | +use crate::encode::{EncodeArgument, EncodeReturn}; |
| 2 | +use crate::rc::Id; |
| 3 | +use crate::runtime::Bool; |
| 4 | +use crate::Message; |
| 5 | + |
| 6 | +mod argument_private { |
| 7 | + pub trait Sealed {} |
| 8 | +} |
| 9 | + |
| 10 | +/// Represents types that can be converted to/from an [`EncodeArgument`] type. |
| 11 | +/// |
| 12 | +/// This is implemented specially for [`bool`] to allow using that as |
| 13 | +/// Objective-C `BOOL`, where it would otherwise not be allowed (since they |
| 14 | +/// are not ABI compatible). |
| 15 | +/// |
| 16 | +/// This is also done specially for `&mut Id<_>`-like arguments, to allow |
| 17 | +/// using those as "out" parameters. |
| 18 | +pub trait ConvertArgument: argument_private::Sealed { |
| 19 | + /// The inner type that this can be converted to and from. |
| 20 | + #[doc(hidden)] |
| 21 | + type __Inner: EncodeArgument; |
| 22 | + |
| 23 | + /// A helper type for out parameters. |
| 24 | + #[doc(hidden)] |
| 25 | + type __StoredBeforeMessage: Sized; |
| 26 | + |
| 27 | + #[doc(hidden)] |
| 28 | + fn __from_declared_param(inner: Self::__Inner) -> Self; |
| 29 | + |
| 30 | + #[doc(hidden)] |
| 31 | + fn __into_argument(self) -> (Self::__Inner, Self::__StoredBeforeMessage); |
| 32 | + |
| 33 | + #[doc(hidden)] |
| 34 | + unsafe fn __process_after_message_send(_stored: Self::__StoredBeforeMessage) {} |
| 35 | +} |
| 36 | + |
| 37 | +// Implemented in writeback.rs |
| 38 | +impl<T: Message> argument_private::Sealed for &mut Id<T> {} |
| 39 | +impl<T: Message> argument_private::Sealed for Option<&mut Id<T>> {} |
| 40 | +impl<T: Message> argument_private::Sealed for &mut Option<Id<T>> {} |
| 41 | +impl<T: Message> argument_private::Sealed for Option<&mut Option<Id<T>>> {} |
| 42 | + |
| 43 | +impl<T: EncodeArgument> argument_private::Sealed for T {} |
| 44 | +impl<T: EncodeArgument> ConvertArgument for T { |
| 45 | + type __Inner = Self; |
| 46 | + |
| 47 | + type __StoredBeforeMessage = (); |
| 48 | + |
| 49 | + #[inline] |
| 50 | + fn __from_declared_param(inner: Self::__Inner) -> Self { |
| 51 | + inner |
| 52 | + } |
| 53 | + |
| 54 | + #[inline] |
| 55 | + fn __into_argument(self) -> (Self::__Inner, Self::__StoredBeforeMessage) { |
| 56 | + (self, ()) |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +impl argument_private::Sealed for bool {} |
| 61 | +impl ConvertArgument for bool { |
| 62 | + type __Inner = Bool; |
| 63 | + |
| 64 | + type __StoredBeforeMessage = (); |
| 65 | + |
| 66 | + #[inline] |
| 67 | + fn __from_declared_param(inner: Self::__Inner) -> Self { |
| 68 | + inner.as_bool() |
| 69 | + } |
| 70 | + |
| 71 | + #[inline] |
| 72 | + fn __into_argument(self) -> (Self::__Inner, Self::__StoredBeforeMessage) { |
| 73 | + (Bool::new(self), ()) |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +mod return_private { |
| 78 | + pub trait Sealed {} |
| 79 | +} |
| 80 | + |
| 81 | +/// Same as [`ConvertArgument`], but for return types. |
| 82 | +pub trait ConvertReturn: return_private::Sealed { |
| 83 | + /// The inner type that this can be converted to and from. |
| 84 | + #[doc(hidden)] |
| 85 | + type __Inner: EncodeReturn; |
| 86 | + |
| 87 | + #[doc(hidden)] |
| 88 | + fn __into_declared_return(self) -> Self::__Inner; |
| 89 | + |
| 90 | + #[doc(hidden)] |
| 91 | + fn __from_return(inner: Self::__Inner) -> Self; |
| 92 | +} |
| 93 | + |
| 94 | +impl<T: EncodeReturn> return_private::Sealed for T {} |
| 95 | +impl<T: EncodeReturn> ConvertReturn for T { |
| 96 | + type __Inner = Self; |
| 97 | + |
| 98 | + #[inline] |
| 99 | + fn __into_declared_return(self) -> Self::__Inner { |
| 100 | + self |
| 101 | + } |
| 102 | + |
| 103 | + #[inline] |
| 104 | + fn __from_return(inner: Self::__Inner) -> Self { |
| 105 | + inner |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +impl return_private::Sealed for bool {} |
| 110 | +impl ConvertReturn for bool { |
| 111 | + type __Inner = Bool; |
| 112 | + |
| 113 | + #[inline] |
| 114 | + fn __into_declared_return(self) -> Self::__Inner { |
| 115 | + Bool::new(self) |
| 116 | + } |
| 117 | + |
| 118 | + #[inline] |
| 119 | + fn __from_return(inner: Self::__Inner) -> Self { |
| 120 | + inner.as_bool() |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +#[cfg(test)] |
| 125 | +mod tests { |
| 126 | + use super::*; |
| 127 | + |
| 128 | + use core::any::TypeId; |
| 129 | + |
| 130 | + #[test] |
| 131 | + fn convert_normally_noop() { |
| 132 | + assert_eq!( |
| 133 | + TypeId::of::<<i32 as ConvertArgument>::__Inner>(), |
| 134 | + TypeId::of::<i32>() |
| 135 | + ); |
| 136 | + assert_eq!(<i32 as ConvertArgument>::__from_declared_param(42), 42); |
| 137 | + assert_eq!(ConvertArgument::__into_argument(42i32).0, 42); |
| 138 | + } |
| 139 | + |
| 140 | + #[test] |
| 141 | + fn convert_i8() { |
| 142 | + assert_eq!( |
| 143 | + TypeId::of::<<i8 as ConvertArgument>::__Inner>(), |
| 144 | + TypeId::of::<i8>() |
| 145 | + ); |
| 146 | + assert_eq!(<i8 as ConvertArgument>::__from_declared_param(-3), -3); |
| 147 | + assert_eq!(ConvertArgument::__into_argument(-3i32).0, -3); |
| 148 | + } |
| 149 | + |
| 150 | + #[test] |
| 151 | + fn convert_bool() { |
| 152 | + assert!(!<bool as ConvertArgument>::__from_declared_param(Bool::NO)); |
| 153 | + assert!(<bool as ConvertArgument>::__from_declared_param(Bool::YES)); |
| 154 | + assert!(!<bool as ConvertReturn>::__from_return(Bool::NO)); |
| 155 | + assert!(<bool as ConvertReturn>::__from_return(Bool::YES)); |
| 156 | + |
| 157 | + assert!(!ConvertArgument::__into_argument(false).0.as_bool()); |
| 158 | + assert!(ConvertArgument::__into_argument(true).0.as_bool()); |
| 159 | + assert!(!ConvertReturn::__into_declared_return(false).as_bool()); |
| 160 | + assert!(ConvertReturn::__into_declared_return(true).as_bool()); |
| 161 | + |
| 162 | + #[cfg(all(feature = "apple", target_os = "macos", target_arch = "x86_64"))] |
| 163 | + assert_eq!( |
| 164 | + <bool as ConvertArgument>::__Inner::ENCODING_ARGUMENT, |
| 165 | + crate::encode::Encoding::Char, |
| 166 | + ); |
| 167 | + } |
| 168 | +} |
0 commit comments