Adds a method with the given name and implementation to self. Panics if the method wasn't sucessfully added or if the selector and function take different numbers of arguments. Unsafe because the caller must ensure that the types match those that are expected when the method is invoked from Objective-C.
(&mut self, sel: Sel, func: F)
| 165 | /// Unsafe because the caller must ensure that the types match those that |
| 166 | /// are expected when the method is invoked from Objective-C. |
| 167 | pub unsafe fn add_method<F>(&mut self, sel: Sel, func: F) |
| 168 | where F: MethodImplementation<Callee=Object> { |
| 169 | let encs = F::Args::encodings(); |
| 170 | let encs = encs.as_ref(); |
| 171 | let sel_args = count_args(sel); |
| 172 | assert!(sel_args == encs.len(), |
| 173 | "Selector accepts {} arguments, but function accepts {}", |
| 174 | sel_args, encs.len(), |
| 175 | ); |
| 176 | |
| 177 | let types = method_type_encoding(&F::Ret::encode(), encs); |
| 178 | let success = runtime::class_addMethod(self.cls, sel, func.imp(), |
| 179 | types.as_ptr()); |
| 180 | assert!(success != NO, "Failed to add method {:?}", sel); |
| 181 | } |
| 182 | |
| 183 | /// Adds a class method with the given name and implementation to self. |
| 184 | /// Panics if the method wasn't sucessfully added |