Skip to content

Commit

Permalink
Support the rest of the super selectors [WIP]
Browse files Browse the repository at this point in the history
  • Loading branch information
madsmtm committed Jun 15, 2022
1 parent 8106a8d commit 3ceff5a
Showing 1 changed file with 60 additions and 1 deletion.
61 changes: 60 additions & 1 deletion objc2/src/__macro_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,23 @@ impl<T: ?Sized + Message, O: Ownership> MsgSendId<&'_ Class, Id<T, O>>
}
}

// Super: `new`, TODO: Can this ever happen?
impl<T: ?Sized + Message, O: Ownership> MsgSendSuperId<&'_ Class, Id<T, O>>
for RetainSemantics<true, false, false, false>
{
#[inline(always)]
unsafe fn send_super_message_id<A: MessageArguments>(
obj: &Class,
superclass: &Class,
sel: Sel,
args: A,
) -> Result<Option<Id<T, O>>, MessageError> {
unsafe {
MessageReceiver::send_super_message(obj, superclass, sel, args).map(|r| Id::new(r))
}
}
}

// `alloc`, should mark the return value as "allocated, not initialized" somehow
impl<T: ?Sized + Message, O: Ownership> MsgSendId<&'_ Class, Id<T, O>>
for RetainSemantics<false, true, false, false>
Expand All @@ -89,6 +106,23 @@ impl<T: ?Sized + Message, O: Ownership> MsgSendId<&'_ Class, Id<T, O>>
}
}

// Super: `alloc`, TODO: Can this ever happen?
impl<T: ?Sized + Message, O: Ownership> MsgSendSuperId<&'_ Class, Id<T, O>>
for RetainSemantics<false, true, false, false>
{
#[inline(always)]
unsafe fn send_super_message_id<A: MessageArguments>(
cls: &Class,
superclass: &Class,
sel: Sel,
args: A,
) -> Result<Option<Id<T, O>>, MessageError> {
unsafe {
MessageReceiver::send_super_message(cls, superclass, sel, args).map(|r| Id::new(r))
}
}
}

// `init`, should mark the input value as "allocated, not initialized" somehow
impl<T: ?Sized + Message, O: Ownership> MsgSendId<Option<Id<T, O>>, Id<T, O>>
for RetainSemantics<false, false, true, false>
Expand Down Expand Up @@ -145,6 +179,23 @@ impl<T: MessageReceiver, U: ?Sized + Message, O: Ownership> MsgSendId<T, Id<U, O
}
}

// Super: `copy` and `mutableCopy`. TODO: Will this ever happen?
impl<T: MessageReceiver, U: ?Sized + Message, O: Ownership> MsgSendSuperId<T, Id<U, O>>
for RetainSemantics<false, false, false, true>
{
#[inline(always)]
unsafe fn send_super_message_id<A: MessageArguments>(
obj: T,
superclass: &Class,
sel: Sel,
args: A,
) -> Result<Option<Id<U, O>>, MessageError> {
unsafe {
MessageReceiver::send_super_message(obj, superclass, sel, args).map(|r| Id::new(r))
}
}
}

// All other selectors
impl<T: MessageReceiver, U: Message, O: Ownership> MsgSendId<T, Id<U, O>>
for RetainSemantics<false, false, false, false>
Expand Down Expand Up @@ -334,17 +385,25 @@ mod tests {
}

#[test]
#[ignore = "TMP"]
fn test_msg_send_super_id() {
// We send the messages to the class itself instead of it's actual
// superclass, just to verify that the macro works.
// TODO: Better solution!
let cls = class!(NSObject);

let obj = unsafe { msg_send_id![cls, alloc] };
let _obj: Id<Object, Owned> = unsafe { msg_send_id![super(cls, cls), new].unwrap() };

let obj = unsafe { msg_send_id![super(cls, cls), alloc] };

let obj = obj.unwrap(); // Required on super
let obj: Id<Object, Owned> = unsafe { msg_send_id![super(obj, cls), init].unwrap() };

let _copy: Id<Object, Shared> = unsafe { msg_send_id![super(&obj, cls), copy].unwrap() };

let _mutable_copy: Id<Object, Shared> =
unsafe { msg_send_id![super(&obj, cls), mutableCopy].unwrap() };

let _desc: Option<Id<Object, Shared>> =
unsafe { msg_send_id![super(&obj, cls), description] };
}
Expand Down

0 comments on commit 3ceff5a

Please sign in to comment.