MCPcopy Create free account
hub / github.com/SSheldon/rust-objc / add_class_method

Method add_class_method

src/declare.rs:188–203  ·  view source on GitHub ↗

Adds a class 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)

Source from the content-addressed store, hash-verified

186 /// Unsafe because the caller must ensure that the types match those that
187 /// are expected when the method is invoked from Objective-C.
188 pub unsafe fn add_class_method<F>(&mut self, sel: Sel, func: F)
189 where F: MethodImplementation<Callee=Class> {
190 let encs = F::Args::encodings();
191 let encs = encs.as_ref();
192 let sel_args = count_args(sel);
193 assert!(sel_args == encs.len(),
194 "Selector accepts {} arguments, but function accepts {}",
195 sel_args, encs.len(),
196 );
197
198 let types = method_type_encoding(&F::Ret::encode(), encs);
199 let metaclass = (*self.cls).metaclass() as *const _ as *mut _;
200 let success = runtime::class_addMethod(metaclass, sel, func.imp(),
201 types.as_ptr());
202 assert!(success != NO, "Failed to add class method {:?}", sel);
203 }
204
205 /// Adds an ivar with type `T` and the provided name to self.
206 /// Panics if the ivar wasn't successfully added.

Callers 2

custom_classFunction · 0.80
rootMethod · 0.80

Calls 4

count_argsFunction · 0.85
method_type_encodingFunction · 0.85
metaclassMethod · 0.80
as_ptrMethod · 0.80

Tested by 1

custom_classFunction · 0.64