mtl_gpu/library/
linked_functions.rs1use std::ffi::c_void;
4use std::ptr::NonNull;
5
6use mtl_foundation::Referencing;
7use mtl_sys::{msg_send_0, msg_send_1, sel};
8
9#[repr(transparent)]
16pub struct LinkedFunctions(pub(crate) NonNull<c_void>);
17
18impl LinkedFunctions {
19 pub fn alloc() -> Option<Self> {
23 unsafe {
24 let cls = mtl_sys::Class::get("MTLLinkedFunctions")?;
25 let ptr: *mut c_void = msg_send_0(cls.as_ptr(), sel!(alloc));
26 Self::from_raw(ptr)
27 }
28 }
29
30 pub fn init(&self) -> Option<Self> {
34 unsafe {
35 let ptr: *mut c_void = msg_send_0(self.as_ptr(), sel!(init));
36 Self::from_raw(ptr)
37 }
38 }
39
40 pub fn new() -> Option<Self> {
42 Self::alloc()?.init()
43 }
44
45 pub fn linked_functions() -> Option<Self> {
49 unsafe {
50 let cls = mtl_sys::Class::get("MTLLinkedFunctions")?;
51 let ptr: *mut c_void = msg_send_0(cls.as_ptr(), sel!(linkedFunctions));
52 if ptr.is_null() {
53 return None;
54 }
55 msg_send_0::<*mut c_void>(ptr as *const c_void, sel!(retain));
56 Self::from_raw(ptr)
57 }
58 }
59
60 #[inline]
66 pub unsafe fn from_raw(ptr: *mut c_void) -> Option<Self> {
67 NonNull::new(ptr).map(Self)
68 }
69
70 #[inline]
72 pub fn as_raw(&self) -> *mut c_void {
73 self.0.as_ptr()
74 }
75
76 pub fn functions_raw(&self) -> *mut c_void {
80 unsafe { msg_send_0(self.as_ptr(), sel!(functions)) }
81 }
82
83 pub fn set_functions_raw(&self, functions: *const c_void) {
87 unsafe {
88 msg_send_1::<(), *const c_void>(self.as_ptr(), sel!(setFunctions:), functions);
89 }
90 }
91
92 pub fn binary_functions_raw(&self) -> *mut c_void {
96 unsafe { msg_send_0(self.as_ptr(), sel!(binaryFunctions)) }
97 }
98
99 pub fn set_binary_functions_raw(&self, functions: *const c_void) {
103 unsafe {
104 msg_send_1::<(), *const c_void>(self.as_ptr(), sel!(setBinaryFunctions:), functions);
105 }
106 }
107
108 pub fn private_functions_raw(&self) -> *mut c_void {
112 unsafe { msg_send_0(self.as_ptr(), sel!(privateFunctions)) }
113 }
114
115 pub fn set_private_functions_raw(&self, functions: *const c_void) {
119 unsafe {
120 msg_send_1::<(), *const c_void>(self.as_ptr(), sel!(setPrivateFunctions:), functions);
121 }
122 }
123
124 pub fn groups_raw(&self) -> *mut c_void {
128 unsafe { msg_send_0(self.as_ptr(), sel!(groups)) }
129 }
130
131 pub fn set_groups_raw(&self, groups: *const c_void) {
135 unsafe {
136 msg_send_1::<(), *const c_void>(self.as_ptr(), sel!(setGroups:), groups);
137 }
138 }
139}
140
141impl Default for LinkedFunctions {
142 fn default() -> Self {
143 Self::new().expect("failed to create LinkedFunctions")
144 }
145}
146
147impl Clone for LinkedFunctions {
148 fn clone(&self) -> Self {
149 unsafe {
150 let ptr: *mut c_void = msg_send_0(self.as_ptr(), sel!(copy));
151 Self::from_raw(ptr).expect("failed to copy LinkedFunctions")
152 }
153 }
154}
155
156impl Drop for LinkedFunctions {
157 fn drop(&mut self) {
158 unsafe {
159 msg_send_0::<()>(self.as_ptr(), sel!(release));
160 }
161 }
162}
163
164impl Referencing for LinkedFunctions {
165 #[inline]
166 fn as_ptr(&self) -> *const c_void {
167 self.0.as_ptr()
168 }
169}
170
171unsafe impl Send for LinkedFunctions {}
172unsafe impl Sync for LinkedFunctions {}
173
174impl std::fmt::Debug for LinkedFunctions {
175 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
176 f.debug_struct("LinkedFunctions").finish()
177 }
178}