Types that may be sent Objective-C messages. For example: objects, classes, and blocks.
| 48 | /// Types that may be sent Objective-C messages. |
| 49 | /// For example: objects, classes, and blocks. |
| 50 | pub unsafe trait Message { |
| 51 | /** |
| 52 | Sends a message to self with the given selector and arguments. |
| 53 | |
| 54 | The correct version of `objc_msgSend` will be chosen based on the |
| 55 | return type. For more information, see Apple's documentation: |
| 56 | <https://developer.apple.com/library/mac/documentation/Cocoa/Reference/ObjCRuntimeRef/index.html#//apple_ref/doc/uid/TP40001418-CH1g-88778> |
| 57 | |
| 58 | If the selector is known at compile-time, it is recommended to use the |
| 59 | `msg_send!` macro rather than this method. |
| 60 | */ |
| 61 | #[cfg(not(feature = "verify_message"))] |
| 62 | unsafe fn send_message<A, R>(&self, sel: Sel, args: A) |
| 63 | -> Result<R, MessageError> |
| 64 | where Self: Sized, A: MessageArguments, R: Any { |
| 65 | send_message(self, sel, args) |
| 66 | } |
| 67 | |
| 68 | #[cfg(feature = "verify_message")] |
| 69 | unsafe fn send_message<A, R>(&self, sel: Sel, args: A) |
| 70 | -> Result<R, MessageError> |
| 71 | where Self: Sized, A: MessageArguments + EncodeArguments, |
| 72 | R: Any + Encode { |
| 73 | send_message(self, sel, args) |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | Verifies that the argument and return types match the encoding of the |
| 78 | method for the given selector. |
| 79 | |
| 80 | This will look up the encoding of the method for the given selector, `sel`, |
| 81 | and return a `MessageError` if any encodings differ for the arguments `A` |
| 82 | and return type `R`. |
| 83 | |
| 84 | # Example |
| 85 | ``` no_run |
| 86 | # #[macro_use] extern crate objc; |
| 87 | # use objc::runtime::{BOOL, Class, Object}; |
| 88 | # use objc::Message; |
| 89 | # fn main() { |
| 90 | let obj: &Object; |
| 91 | # obj = unsafe { msg_send![class!(NSObject), new] }; |
| 92 | let sel = sel!(isKindOfClass:); |
| 93 | // Verify isKindOfClass: takes one Class and returns a BOOL |
| 94 | let result = obj.verify_message::<(&Class,), BOOL>(sel); |
| 95 | assert!(result.is_ok()); |
| 96 | # } |
| 97 | ``` |
| 98 | */ |
| 99 | fn verify_message<A, R>(&self, sel: Sel) -> Result<(), MessageError> |
| 100 | where Self: Sized, A: EncodeArguments, R: Encode { |
| 101 | let obj = unsafe { &*(self as *const _ as *const Object) }; |
| 102 | verify_message_signature::<A, R>(obj.class(), sel) |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | unsafe impl Message for Object { } |
| 107 |
nothing calls this directly
no outgoing calls
no test coverage detected