Skip to main content

mtl_gpu/library/
function_reflection.rs

1//! Function reflection information.
2
3use std::ffi::c_void;
4use std::ptr::NonNull;
5
6use mtl_foundation::Referencing;
7use mtl_sys::{msg_send_0, sel};
8
9/// Function reflection information.
10///
11/// C++ equivalent: `MTL::FunctionReflection`
12///
13/// Contains reflection information about a function.
14#[repr(transparent)]
15pub struct FunctionReflection(pub(crate) NonNull<c_void>);
16
17impl FunctionReflection {
18    /// Allocate a new function reflection.
19    ///
20    /// C++ equivalent: `static FunctionReflection* alloc()`
21    pub fn alloc() -> Option<Self> {
22        unsafe {
23            let cls = mtl_sys::Class::get("MTLFunctionReflection")?;
24            let ptr: *mut c_void = msg_send_0(cls.as_ptr(), sel!(alloc));
25            Self::from_raw(ptr)
26        }
27    }
28
29    /// Initialize an allocated function reflection.
30    ///
31    /// C++ equivalent: `FunctionReflection* init()`
32    pub fn init(&self) -> Option<Self> {
33        unsafe {
34            let ptr: *mut c_void = msg_send_0(self.as_ptr(), sel!(init));
35            Self::from_raw(ptr)
36        }
37    }
38
39    /// Create a new function reflection.
40    pub fn new() -> Option<Self> {
41        Self::alloc()?.init()
42    }
43
44    /// Create from a raw pointer.
45    ///
46    /// # Safety
47    ///
48    /// The pointer must be a valid Metal function reflection object.
49    #[inline]
50    pub unsafe fn from_raw(ptr: *mut c_void) -> Option<Self> {
51        NonNull::new(ptr).map(Self)
52    }
53
54    /// Get the raw pointer.
55    #[inline]
56    pub fn as_raw(&self) -> *mut c_void {
57        self.0.as_ptr()
58    }
59
60    /// Get the function bindings (raw NSArray pointer).
61    ///
62    /// C++ equivalent: `NS::Array* bindings() const`
63    pub fn bindings_raw(&self) -> *mut c_void {
64        unsafe { msg_send_0(self.as_ptr(), sel!(bindings)) }
65    }
66}
67
68impl Clone for FunctionReflection {
69    fn clone(&self) -> Self {
70        unsafe {
71            msg_send_0::<*mut c_void>(self.as_ptr(), sel!(retain));
72        }
73        Self(self.0)
74    }
75}
76
77impl Drop for FunctionReflection {
78    fn drop(&mut self) {
79        unsafe {
80            msg_send_0::<()>(self.as_ptr(), sel!(release));
81        }
82    }
83}
84
85impl Referencing for FunctionReflection {
86    #[inline]
87    fn as_ptr(&self) -> *const c_void {
88        self.0.as_ptr()
89    }
90}
91
92unsafe impl Send for FunctionReflection {}
93unsafe impl Sync for FunctionReflection {}
94
95impl std::fmt::Debug for FunctionReflection {
96    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
97        f.debug_struct("FunctionReflection").finish()
98    }
99}