Trait objc2::runtime::MessageReceiver
source · pub unsafe trait MessageReceiver: Sealed + Sized {
// Provided methods
unsafe fn send_message<A: EncodeArguments, R: EncodeReturn>(
self,
sel: Sel,
args: A
) -> R { ... }
unsafe fn send_super_message<A: EncodeArguments, R: EncodeReturn>(
self,
superclass: &AnyClass,
sel: Sel,
args: A
) -> R { ... }
}
Expand description
Types that can directly be used as the receiver of Objective-C messages.
Examples include objects pointers, class pointers, and block pointers.
§Safety
This is a sealed trait, and should not need to be implemented. Open an issue if you know a use-case where this restrition should be lifted!
Provided Methods§
sourceunsafe fn send_message<A: EncodeArguments, R: EncodeReturn>(
self,
sel: Sel,
args: A
) -> R
unsafe fn send_message<A: EncodeArguments, R: EncodeReturn>( self, sel: Sel, args: A ) -> R
Sends a message to the receiver with the given selector and arguments.
This should be used instead of the performSelector:
family of
methods, as this is both more performant and flexible than that.
The correct version of objc_msgSend
will be chosen based on the
return type. For more information, see the Messaging section in
Apple’s Objective-C Runtime Programming Guide.
If the selector is known at compile-time, it is recommended to use the
msg_send!
macro rather than this method.
§Safety
This shares the same safety requirements as msg_send!
.
The added invariant is that the selector must take the same number of arguments as is given.
§Example
Call the copy
method, but using a dynamic selector instead.
use objc2::rc::Retained;
use objc2::runtime::MessageReceiver;
use objc2::sel;
let obj = MyObject::new();
// SAFETY: The `copy` method takes no arguments, and returns an object
let copy: *mut MyObject = unsafe { obj.send_message(sel!(copy), ()) };
// SAFETY: The `copy` method returns an object with +1 retain count
let copy = unsafe { Retained::from_raw(copy) }.unwrap();
sourceunsafe fn send_super_message<A: EncodeArguments, R: EncodeReturn>(
self,
superclass: &AnyClass,
sel: Sel,
args: A
) -> R
unsafe fn send_super_message<A: EncodeArguments, R: EncodeReturn>( self, superclass: &AnyClass, sel: Sel, args: A ) -> R
Sends a message to a specific superclass with the given selector and arguments.
The correct version of objc_msgSend_super
will be chosen based on the
return type. For more information, see the section on “Sending
Messages” in Apple’s documentation.
If the selector is known at compile-time, it is recommended to use the
msg_send!(super(...), ...)
macro rather than this method.
§Safety
This shares the same safety requirements as
msg_send!(super(...), ...)
.
The added invariant is that the selector must take the same number of arguments as is given.