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.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.
Implementations on Foreign Types§
impl<T: ?Sized + Message> MessageReceiver for *const T
impl<T: ?Sized + Message> MessageReceiver for *mut T
impl<T: ?Sized + Message> MessageReceiver for &T
impl<T: ?Sized + Message> MessageReceiver for NonNull<T>
Implementors§
impl MessageReceiver for &mut AnyObject
&mut AnyObject
is allowed as mutable, for easier transition from objc
,
even though it’s basically always incorrect to hold &mut AnyObject
.
Use *mut AnyObject
instead if you know for certain you need mutability,
and cannot make do with interior mutability.