Skip to main content

mtl_gpu/mtl4/compiler/
task_options.rs

1//! MTL4 Compiler task options.
2
3use 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/// Options for compiler tasks.
10///
11/// C++ equivalent: `MTL4::CompilerTaskOptions`
12#[repr(transparent)]
13pub struct CompilerTaskOptions(NonNull<c_void>);
14
15impl CompilerTaskOptions {
16    /// Create a CompilerTaskOptions from a raw pointer.
17    #[inline]
18    pub unsafe fn from_raw(ptr: *mut c_void) -> Option<Self> {
19        NonNull::new(ptr).map(Self)
20    }
21
22    /// Get the raw pointer.
23    #[inline]
24    pub fn as_raw(&self) -> *mut c_void {
25        self.0.as_ptr()
26    }
27
28    /// Create new compiler task options.
29    pub fn new() -> Option<Self> {
30        unsafe {
31            let class = mtl_sys::Class::get("MTL4CompilerTaskOptions")?;
32            let ptr: *mut c_void = msg_send_0(class.as_ptr(), sel!(alloc));
33            if ptr.is_null() {
34                return None;
35            }
36            let ptr: *mut c_void = msg_send_0(ptr, sel!(init));
37            Self::from_raw(ptr)
38        }
39    }
40
41    /// Get the lookup archives (as raw pointer to NSArray).
42    ///
43    /// C++ equivalent: `NS::Array* lookupArchives() const`
44    pub fn lookup_archives_raw(&self) -> *mut c_void {
45        unsafe { msg_send_0(self.as_ptr(), sel!(lookupArchives)) }
46    }
47
48    /// Set the lookup archives (from raw pointer to NSArray).
49    ///
50    /// C++ equivalent: `void setLookupArchives(const NS::Array*)`
51    pub fn set_lookup_archives_raw(&self, archives: *const c_void) {
52        unsafe {
53            let _: () = msg_send_1(self.as_ptr(), sel!(setLookupArchives:), archives);
54        }
55    }
56}
57
58impl Clone for CompilerTaskOptions {
59    fn clone(&self) -> Self {
60        unsafe {
61            mtl_sys::msg_send_0::<*mut c_void>(self.as_ptr(), mtl_sys::sel!(retain));
62        }
63        Self(self.0)
64    }
65}
66
67impl Drop for CompilerTaskOptions {
68    fn drop(&mut self) {
69        unsafe {
70            mtl_sys::msg_send_0::<()>(self.as_ptr(), mtl_sys::sel!(release));
71        }
72    }
73}
74
75impl Referencing for CompilerTaskOptions {
76    #[inline]
77    fn as_ptr(&self) -> *const c_void {
78        self.0.as_ptr()
79    }
80}
81
82unsafe impl Send for CompilerTaskOptions {}
83unsafe impl Sync for CompilerTaskOptions {}
84
85impl std::fmt::Debug for CompilerTaskOptions {
86    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
87        f.debug_struct("CompilerTaskOptions").finish()
88    }
89}