Skip to content

Commit 829dc2f

Browse files
committed
Remove MessageArguments in favour of ConvertArguments
To hide more implementation details, and to in the future make it possible to always do the fast autorelease optimization, even when working with out parameters.
1 parent 57f97bf commit 829dc2f

File tree

16 files changed

+414
-441
lines changed

16 files changed

+414
-441
lines changed

crates/objc2/CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
2828
who are `IsAllocableAnyThread`.
2929
* **BREAKING**: Moved the `MethodImplementation` trait from the `declare`
3030
module to the `runtime` module.
31-
* **BREAKING**: Moved the `MessageReceiver` and `MessageArguments` traits to
32-
the `runtime` module.
31+
* **BREAKING**: Moved the `MessageReceiver` trait to the `runtime` module.
3332

3433
### Deprecated
3534
* Soft deprecated using `msg_send!` without a comma between arguments (i.e.
@@ -64,6 +63,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
6463
### Removed
6564
* **BREAKING**: Removed `ProtocolType` implementation for `NSObject`.
6665
Use the more precise `NSObjectProtocol` trait instead!
66+
* **BREAKING**: Removed the `MessageArguments` trait.
6767

6868

6969
## 0.4.1 - 2023-07-31

crates/objc2/src/__macro_helpers/convert.rs

Lines changed: 163 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::encode::{EncodeArgument, EncodeReturn};
1+
use crate::encode::{EncodeArgument, EncodeArguments, EncodeReturn};
22
use crate::rc::Id;
33
use crate::runtime::Bool;
44
use crate::Message;
@@ -31,6 +31,7 @@ pub trait ConvertArgument: argument_private::Sealed {
3131
fn __into_argument(self) -> (Self::__Inner, Self::__StoredBeforeMessage);
3232

3333
#[doc(hidden)]
34+
#[inline]
3435
unsafe fn __process_after_message_send(_stored: Self::__StoredBeforeMessage) {}
3536
}
3637

@@ -121,6 +122,167 @@ impl ConvertReturn for bool {
121122
}
122123
}
123124

125+
pub trait ConvertArguments {
126+
#[doc(hidden)]
127+
type __Inner: EncodeArguments;
128+
129+
#[doc(hidden)]
130+
type __StoredBeforeMessage: Sized;
131+
132+
#[doc(hidden)]
133+
fn __into_arguments(self) -> (Self::__Inner, Self::__StoredBeforeMessage);
134+
135+
#[doc(hidden)]
136+
unsafe fn __process_after_message_send(_stored: Self::__StoredBeforeMessage);
137+
}
138+
139+
pub trait TupleExtender<T> {
140+
#[doc(hidden)]
141+
type PlusOneArgument;
142+
#[doc(hidden)]
143+
fn add_argument(self, arg: T) -> Self::PlusOneArgument;
144+
}
145+
146+
macro_rules! args_impl {
147+
($($a:ident: $t:ident),*) => (
148+
impl<$($t: ConvertArgument),*> ConvertArguments for ($($t,)*) {
149+
type __Inner = ($($t::__Inner,)*);
150+
151+
type __StoredBeforeMessage = ($($t::__StoredBeforeMessage,)*);
152+
153+
#[inline]
154+
fn __into_arguments(self) -> (Self::__Inner, Self::__StoredBeforeMessage) {
155+
let ($($a,)*) = self;
156+
$(let $a = ConvertArgument::__into_argument($a);)*
157+
158+
(($($a.0,)*), ($($a.1,)*))
159+
}
160+
161+
#[inline]
162+
unsafe fn __process_after_message_send(($($a,)*): Self::__StoredBeforeMessage) {
163+
$(
164+
unsafe { <$t as ConvertArgument>::__process_after_message_send($a) };
165+
)*
166+
}
167+
}
168+
169+
impl<$($t,)* T> TupleExtender<T> for ($($t,)*) {
170+
type PlusOneArgument = ($($t,)* T,);
171+
172+
#[inline]
173+
fn add_argument(self, arg: T) -> Self::PlusOneArgument {
174+
let ($($a,)*) = self;
175+
($($a,)* arg,)
176+
}
177+
}
178+
);
179+
}
180+
181+
args_impl!();
182+
args_impl!(a: A);
183+
args_impl!(a: A, b: B);
184+
args_impl!(a: A, b: B, c: C);
185+
args_impl!(a: A, b: B, c: C, d: D);
186+
args_impl!(a: A, b: B, c: C, d: D, e: E);
187+
args_impl!(a: A, b: B, c: C, d: D, e: E, f: F);
188+
args_impl!(a: A, b: B, c: C, d: D, e: E, f: F, g: G);
189+
args_impl!(a: A, b: B, c: C, d: D, e: E, f: F, g: G, h: H);
190+
args_impl!(a: A, b: B, c: C, d: D, e: E, f: F, g: G, h: H, i: I);
191+
args_impl!(a: A, b: B, c: C, d: D, e: E, f: F, g: G, h: H, i: I, j: J);
192+
args_impl!(
193+
a: A,
194+
b: B,
195+
c: C,
196+
d: D,
197+
e: E,
198+
f: F,
199+
g: G,
200+
h: H,
201+
i: I,
202+
j: J,
203+
k: K
204+
);
205+
args_impl!(
206+
a: A,
207+
b: B,
208+
c: C,
209+
d: D,
210+
e: E,
211+
f: F,
212+
g: G,
213+
h: H,
214+
i: I,
215+
j: J,
216+
k: K,
217+
l: L
218+
);
219+
args_impl!(
220+
a: A,
221+
b: B,
222+
c: C,
223+
d: D,
224+
e: E,
225+
f: F,
226+
g: G,
227+
h: H,
228+
i: I,
229+
j: J,
230+
k: K,
231+
l: L,
232+
m: M
233+
);
234+
args_impl!(
235+
a: A,
236+
b: B,
237+
c: C,
238+
d: D,
239+
e: E,
240+
f: F,
241+
g: G,
242+
h: H,
243+
i: I,
244+
j: J,
245+
k: K,
246+
l: L,
247+
m: M,
248+
n: N
249+
);
250+
args_impl!(
251+
a: A,
252+
b: B,
253+
c: C,
254+
d: D,
255+
e: E,
256+
f: F,
257+
g: G,
258+
h: H,
259+
i: I,
260+
j: J,
261+
k: K,
262+
l: L,
263+
m: M,
264+
n: N,
265+
o: O
266+
);
267+
args_impl!(
268+
a: A,
269+
b: B,
270+
c: C,
271+
d: D,
272+
e: E,
273+
f: F,
274+
g: G,
275+
h: H,
276+
i: I,
277+
j: J,
278+
k: K,
279+
l: L,
280+
m: M,
281+
n: N,
282+
o: O,
283+
p: P
284+
);
285+
124286
#[cfg(test)]
125287
mod tests {
126288
use super::*;

crates/objc2/src/__macro_helpers/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ mod writeback;
3232

3333
pub use self::cache::{CachedClass, CachedSel};
3434
pub use self::common_selectors::{alloc_sel, dealloc_sel, init_sel, new_sel};
35-
pub use self::convert::{ConvertArgument, ConvertReturn};
35+
pub use self::convert::{ConvertArgument, ConvertArguments, ConvertReturn, TupleExtender};
3636
pub use self::declare_class::{
3737
assert_mutability_matches_superclass_mutability, IdReturnValue, MaybeOptionId,
3838
MessageRecieveId, ValidSubclassMutability,

crates/objc2/src/__macro_helpers/msg_send_id.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@ use core::ptr;
22

33
use crate::encode::Encode;
44
use crate::rc::{Allocated, Id};
5-
use crate::runtime::message::__TupleExtender;
6-
use crate::runtime::{AnyClass, AnyObject, MessageArguments, MessageReceiver, Sel};
5+
use crate::runtime::{AnyClass, AnyObject, MessageReceiver, Sel};
76
use crate::{sel, Message};
87

9-
use super::{Alloc, CopyOrMutCopy, Init, New, Other};
8+
use super::{Alloc, ConvertArguments, CopyOrMutCopy, Init, New, Other, TupleExtender};
109

1110
pub trait MsgSendId<T, U> {
1211
#[track_caller]
13-
unsafe fn send_message_id<A: MessageArguments, R: MaybeUnwrap<Input = U>>(
12+
unsafe fn send_message_id<A: ConvertArguments, R: MaybeUnwrap<Input = U>>(
1413
obj: T,
1514
sel: Sel,
1615
args: A,
@@ -23,8 +22,8 @@ pub trait MsgSendId<T, U> {
2322
unsafe fn send_message_id_error<A, E>(obj: T, sel: Sel, args: A) -> Result<U, Id<E>>
2423
where
2524
*mut *mut E: Encode,
26-
A: __TupleExtender<*mut *mut E>,
27-
<A as __TupleExtender<*mut *mut E>>::PlusOneArgument: MessageArguments,
25+
A: TupleExtender<*mut *mut E>,
26+
<A as TupleExtender<*mut *mut E>>::PlusOneArgument: ConvertArguments,
2827
E: Message,
2928
Option<U>: MaybeUnwrap<Input = U>,
3029
{
@@ -69,7 +68,7 @@ unsafe fn encountered_error<E: Message>(err: *mut E) -> Id<E> {
6968

7069
impl<T: MessageReceiver, U: ?Sized + Message> MsgSendId<T, Id<U>> for New {
7170
#[inline]
72-
unsafe fn send_message_id<A: MessageArguments, R: MaybeUnwrap<Input = Id<U>>>(
71+
unsafe fn send_message_id<A: ConvertArguments, R: MaybeUnwrap<Input = Id<U>>>(
7372
obj: T,
7473
sel: Sel,
7574
args: A,
@@ -88,7 +87,7 @@ impl<T: MessageReceiver, U: ?Sized + Message> MsgSendId<T, Id<U>> for New {
8887

8988
impl<T: ?Sized + Message> MsgSendId<&'_ AnyClass, Allocated<T>> for Alloc {
9089
#[inline]
91-
unsafe fn send_message_id<A: MessageArguments, R: MaybeUnwrap<Input = Allocated<T>>>(
90+
unsafe fn send_message_id<A: ConvertArguments, R: MaybeUnwrap<Input = Allocated<T>>>(
9291
cls: &AnyClass,
9392
sel: Sel,
9493
args: A,
@@ -103,7 +102,7 @@ impl<T: ?Sized + Message> MsgSendId<&'_ AnyClass, Allocated<T>> for Alloc {
103102

104103
impl<T: ?Sized + Message> MsgSendId<Option<Allocated<T>>, Id<T>> for Init {
105104
#[inline]
106-
unsafe fn send_message_id<A: MessageArguments, R: MaybeUnwrap<Input = Id<T>>>(
105+
unsafe fn send_message_id<A: ConvertArguments, R: MaybeUnwrap<Input = Id<T>>>(
107106
obj: Option<Allocated<T>>,
108107
sel: Sel,
109108
args: A,
@@ -124,7 +123,7 @@ impl<T: ?Sized + Message> MsgSendId<Option<Allocated<T>>, Id<T>> for Init {
124123

125124
impl<T: MessageReceiver, U: ?Sized + Message> MsgSendId<T, Id<U>> for CopyOrMutCopy {
126125
#[inline]
127-
unsafe fn send_message_id<A: MessageArguments, R: MaybeUnwrap<Input = Id<U>>>(
126+
unsafe fn send_message_id<A: ConvertArguments, R: MaybeUnwrap<Input = Id<U>>>(
128127
obj: T,
129128
sel: Sel,
130129
args: A,
@@ -140,7 +139,7 @@ impl<T: MessageReceiver, U: ?Sized + Message> MsgSendId<T, Id<U>> for CopyOrMutC
140139

141140
impl<T: MessageReceiver, U: Message> MsgSendId<T, Id<U>> for Other {
142141
#[inline]
143-
unsafe fn send_message_id<A: MessageArguments, R: MaybeUnwrap<Input = Id<U>>>(
142+
unsafe fn send_message_id<A: ConvertArguments, R: MaybeUnwrap<Input = Id<U>>>(
144143
obj: T,
145144
sel: Sel,
146145
args: A,

0 commit comments

Comments
 (0)