Trait objc2::MessageReceiver [−][src]
pub unsafe trait MessageReceiver: Sealed {
fn as_raw_receiver(&self) -> *mut Object;
unsafe fn send_message<A, R>(
&self,
sel: Sel,
args: A
) -> Result<R, MessageError>
where
A: MessageArguments,
R: Encode,
{ ... }
unsafe fn send_super_message<A, R>(
&self,
superclass: &Class,
sel: Sel,
args: A
) -> Result<R, MessageError>
where
A: MessageArguments,
R: Encode,
{ ... }
fn verify_message<A, R>(&self, sel: Sel) -> Result<(), MessageError>
where
A: EncodeArguments,
R: Encode,
{ ... }
}
Expand description
Types that can directly be used as the receiver of Objective-C messages.
This is a sealed trait (for now) that is automatically implemented for
pointers to types implementing Message
, so that code can be generic
over the message receiver.
This is mostly an implementation detail; you’ll want to implement
Message
for your type instead.
Safety
Self::as_raw_receiver
must be implemented correctly.
Required methods
fn as_raw_receiver(&self) -> *mut Object
fn as_raw_receiver(&self) -> *mut Object
Get a raw pointer to the receiver of the message.
Provided methods
unsafe fn send_message<A, R>(
&self,
sel: Sel,
args: A
) -> Result<R, MessageError> where
A: MessageArguments,
R: Encode,
unsafe fn send_message<A, R>(
&self,
sel: Sel,
args: A
) -> Result<R, MessageError> where
A: MessageArguments,
R: Encode,
Sends a message to self with the given selector and arguments.
The correct version of objc_msgSend
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!
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.
unsafe fn send_super_message<A, R>(
&self,
superclass: &Class,
sel: Sel,
args: A
) -> Result<R, MessageError> where
A: MessageArguments,
R: Encode,
unsafe fn send_super_message<A, R>(
&self,
superclass: &Class,
sel: Sel,
args: A
) -> Result<R, MessageError> where
A: MessageArguments,
R: Encode,
Sends a message to self’s 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.
fn verify_message<A, R>(&self, sel: Sel) -> Result<(), MessageError> where
A: EncodeArguments,
R: Encode,
fn verify_message<A, R>(&self, sel: Sel) -> Result<(), MessageError> where
A: EncodeArguments,
R: Encode,
Verify that the argument and return types match the encoding of the method for the given selector.
This will look up the encoding of the method for the given selector,
sel
, and return a MessageError
if any encodings differ for the
arguments A
and return type R
.
Example
let obj: &Object;
let sel = sel!(isKindOfClass:);
// Verify isKindOfClass: takes one Class and returns a BOOL
let result = obj.verify_message::<(&Class,), Bool>(sel);
assert!(result.is_ok());