Skip to main content

mtl_gpu/argument/
array_type.rs

1//! An array type for reflection.
2
3use std::ffi::c_void;
4use std::ptr::NonNull;
5
6use mtl_foundation::{Referencing, UInteger};
7use mtl_sys::{msg_send_0, sel};
8
9use crate::enums::DataType;
10
11use super::{PointerType, StructType, TensorReferenceType, TextureReferenceType};
12
13/// An array type for reflection.
14///
15/// C++ equivalent: `MTL::ArrayType`
16#[repr(transparent)]
17pub struct ArrayType(pub(crate) NonNull<c_void>);
18
19impl ArrayType {
20    /// Create from a raw pointer.
21    ///
22    /// # Safety
23    ///
24    /// The pointer must be a valid Metal ArrayType.
25    #[inline]
26    pub unsafe fn from_raw(ptr: *mut c_void) -> Option<Self> {
27        NonNull::new(ptr).map(Self)
28    }
29
30    /// Get the raw pointer.
31    #[inline]
32    pub fn as_raw(&self) -> *mut c_void {
33        self.0.as_ptr()
34    }
35
36    /// Get the data type.
37    ///
38    /// C++ equivalent: `DataType dataType() const`
39    #[inline]
40    pub fn data_type(&self) -> DataType {
41        unsafe { msg_send_0(self.as_ptr(), sel!(dataType)) }
42    }
43
44    /// Get the argument index stride.
45    ///
46    /// C++ equivalent: `NS::UInteger argumentIndexStride() const`
47    #[inline]
48    pub fn argument_index_stride(&self) -> UInteger {
49        unsafe { msg_send_0(self.as_ptr(), sel!(argumentIndexStride)) }
50    }
51
52    /// Get the array length.
53    ///
54    /// C++ equivalent: `NS::UInteger arrayLength() const`
55    #[inline]
56    pub fn array_length(&self) -> UInteger {
57        unsafe { msg_send_0(self.as_ptr(), sel!(arrayLength)) }
58    }
59
60    /// Get the element type.
61    ///
62    /// C++ equivalent: `DataType elementType() const`
63    #[inline]
64    pub fn element_type(&self) -> DataType {
65        unsafe { msg_send_0(self.as_ptr(), sel!(elementType)) }
66    }
67
68    /// Get the stride.
69    ///
70    /// C++ equivalent: `NS::UInteger stride() const`
71    #[inline]
72    pub fn stride(&self) -> UInteger {
73        unsafe { msg_send_0(self.as_ptr(), sel!(stride)) }
74    }
75
76    /// Get the element array type (if element is an array).
77    ///
78    /// C++ equivalent: `ArrayType* elementArrayType()`
79    pub fn element_array_type(&self) -> Option<ArrayType> {
80        unsafe {
81            let ptr: *mut c_void = msg_send_0(self.as_ptr(), sel!(elementArrayType));
82            ArrayType::from_raw(ptr)
83        }
84    }
85
86    /// Get the element pointer type (if element is a pointer).
87    ///
88    /// C++ equivalent: `PointerType* elementPointerType()`
89    pub fn element_pointer_type(&self) -> Option<PointerType> {
90        unsafe {
91            let ptr: *mut c_void = msg_send_0(self.as_ptr(), sel!(elementPointerType));
92            PointerType::from_raw(ptr)
93        }
94    }
95
96    /// Get the element struct type (if element is a struct).
97    ///
98    /// C++ equivalent: `StructType* elementStructType()`
99    pub fn element_struct_type(&self) -> Option<StructType> {
100        unsafe {
101            let ptr: *mut c_void = msg_send_0(self.as_ptr(), sel!(elementStructType));
102            StructType::from_raw(ptr)
103        }
104    }
105
106    /// Get the element texture reference type (if element is a texture).
107    ///
108    /// C++ equivalent: `TextureReferenceType* elementTextureReferenceType()`
109    pub fn element_texture_reference_type(&self) -> Option<TextureReferenceType> {
110        unsafe {
111            let ptr: *mut c_void = msg_send_0(self.as_ptr(), sel!(elementTextureReferenceType));
112            TextureReferenceType::from_raw(ptr)
113        }
114    }
115
116    /// Get the element tensor reference type (if element is a tensor).
117    ///
118    /// C++ equivalent: `TensorReferenceType* elementTensorReferenceType()`
119    pub fn element_tensor_reference_type(&self) -> Option<TensorReferenceType> {
120        unsafe {
121            let ptr: *mut c_void = msg_send_0(self.as_ptr(), sel!(elementTensorReferenceType));
122            TensorReferenceType::from_raw(ptr)
123        }
124    }
125}
126
127impl Clone for ArrayType {
128    fn clone(&self) -> Self {
129        unsafe {
130            msg_send_0::<*mut c_void>(self.as_ptr(), sel!(retain));
131        }
132        Self(self.0)
133    }
134}
135
136impl Drop for ArrayType {
137    fn drop(&mut self) {
138        unsafe {
139            msg_send_0::<()>(self.as_ptr(), sel!(release));
140        }
141    }
142}
143
144impl Referencing for ArrayType {
145    #[inline]
146    fn as_ptr(&self) -> *const c_void {
147        self.0.as_ptr()
148    }
149}
150
151unsafe impl Send for ArrayType {}
152unsafe impl Sync for ArrayType {}