Skip to main content

mtl_gpu/argument/
type_info.rs

1//! Base type for Metal type reflection.
2
3use std::ffi::c_void;
4use std::ptr::NonNull;
5
6use mtl_foundation::Referencing;
7use mtl_sys::{msg_send_0, sel};
8
9use crate::enums::DataType;
10
11/// Base type for Metal type reflection.
12///
13/// C++ equivalent: `MTL::Type`
14#[repr(transparent)]
15pub struct Type(pub(crate) NonNull<c_void>);
16
17impl Type {
18    /// Create from a raw pointer.
19    ///
20    /// # Safety
21    ///
22    /// The pointer must be a valid Metal Type.
23    #[inline]
24    pub unsafe fn from_raw(ptr: *mut c_void) -> Option<Self> {
25        NonNull::new(ptr).map(Self)
26    }
27
28    /// Get the raw pointer.
29    #[inline]
30    pub fn as_raw(&self) -> *mut c_void {
31        self.0.as_ptr()
32    }
33
34    /// Get the data type.
35    ///
36    /// C++ equivalent: `DataType dataType() const`
37    #[inline]
38    pub fn data_type(&self) -> DataType {
39        unsafe { msg_send_0(self.as_ptr(), sel!(dataType)) }
40    }
41}
42
43impl Clone for Type {
44    fn clone(&self) -> Self {
45        unsafe {
46            msg_send_0::<*mut c_void>(self.as_ptr(), sel!(retain));
47        }
48        Self(self.0)
49    }
50}
51
52impl Drop for Type {
53    fn drop(&mut self) {
54        unsafe {
55            msg_send_0::<()>(self.as_ptr(), sel!(release));
56        }
57    }
58}
59
60impl Referencing for Type {
61    #[inline]
62    fn as_ptr(&self) -> *const c_void {
63        self.0.as_ptr()
64    }
65}
66
67unsafe impl Send for Type {}
68unsafe impl Sync for Type {}