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

Method add_method

src/declare.rs:167–181  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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

Callers 2

custom_classFunction · 0.80
custom_subclassFunction · 0.80

Calls 3

count_argsFunction · 0.85
method_type_encodingFunction · 0.85
as_ptrMethod · 0.80

Tested by 2

custom_classFunction · 0.64
custom_subclassFunction · 0.64