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

Function custom_class

src/test_utils.rs:65–122  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

63}
64
65pub fn custom_class() -> &'static Class {
66 static REGISTER_CUSTOM_CLASS: Once = ONCE_INIT;
67
68 REGISTER_CUSTOM_CLASS.call_once(|| {
69 // The runtime will call this method, so it has to be implemented
70 extern fn custom_obj_class_initialize(_this: &Class, _cmd: Sel) { }
71
72 let mut decl = ClassDecl::root("CustomObject", custom_obj_class_initialize).unwrap();
73 let proto = custom_protocol();
74
75 decl.add_protocol(proto);
76 decl.add_ivar::<u32>("_foo");
77
78 extern fn custom_obj_set_foo(this: &mut Object, _cmd: Sel, foo: u32) {
79 unsafe { this.set_ivar::<u32>("_foo", foo); }
80 }
81
82 extern fn custom_obj_get_foo(this: &Object, _cmd: Sel) -> u32 {
83 unsafe { *this.get_ivar::<u32>("_foo") }
84 }
85
86 extern fn custom_obj_get_struct(_this: &Object, _cmd: Sel) -> CustomStruct {
87 CustomStruct { a: 1, b: 2, c: 3, d: 4 }
88 }
89
90 extern fn custom_obj_class_method(_this: &Class, _cmd: Sel) -> u32 {
91 7
92 }
93
94 extern fn custom_obj_set_bar(this: &mut Object, _cmd: Sel, bar: u32) {
95 unsafe { this.set_ivar::<u32>("_foo", bar) ;}
96 }
97
98 extern fn custom_obj_add_number_to_number(_this: &Class, _cmd: Sel, fst: i32, snd: i32) -> i32 {
99 fst + snd
100 }
101
102 unsafe {
103 let set_foo: extern fn(&mut Object, Sel, u32) = custom_obj_set_foo;
104 decl.add_method(sel!(setFoo:), set_foo);
105 let get_foo: extern fn(&Object, Sel) -> u32 = custom_obj_get_foo;
106 decl.add_method(sel!(foo), get_foo);
107 let get_struct: extern fn(&Object, Sel) -> CustomStruct = custom_obj_get_struct;
108 decl.add_method(sel!(customStruct), get_struct);
109 let class_method: extern fn(&Class, Sel) -> u32 = custom_obj_class_method;
110 decl.add_class_method(sel!(classFoo), class_method);
111
112 let protocol_instance_method: extern fn(&mut Object, Sel, u32) = custom_obj_set_bar;
113 decl.add_method(sel!(setBar:), protocol_instance_method);
114 let protocol_class_method: extern fn(&Class, Sel, i32, i32) -> i32 = custom_obj_add_number_to_number;
115 decl.add_class_method(sel!(addNumber:toNumber:), protocol_class_method);
116 }
117
118 decl.register();
119 });
120
121 class!(CustomObject)
122}

Callers 9

test_ivarFunction · 0.85
test_methodFunction · 0.85
test_classFunction · 0.85
test_protocolFunction · 0.85
test_protocol_methodFunction · 0.85
custom_objectFunction · 0.85
custom_subclassFunction · 0.85
test_class_methodFunction · 0.85
test_send_message_superFunction · 0.85

Calls 5

custom_protocolFunction · 0.85
add_protocolMethod · 0.80
add_methodMethod · 0.80
add_class_methodMethod · 0.80
registerMethod · 0.45

Tested by

no test coverage detected