Skip to main content

mtl_gpu/io/
file_handle.rs

1//! IO file handle for Metal.
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/// Handle to a file for IO operations.
10///
11/// C++ equivalent: `MTL::IOFileHandle`
12#[repr(transparent)]
13pub struct IOFileHandle(pub(crate) NonNull<c_void>);
14
15impl IOFileHandle {
16    /// Create from a raw pointer.
17    ///
18    /// # Safety
19    ///
20    /// The pointer must be a valid Metal IO file handle.
21    #[inline]
22    pub unsafe fn from_raw(ptr: *mut c_void) -> Option<Self> {
23        NonNull::new(ptr).map(Self)
24    }
25
26    /// Get the raw pointer.
27    #[inline]
28    pub fn as_raw(&self) -> *mut c_void {
29        self.0.as_ptr()
30    }
31
32    /// Get the label.
33    ///
34    /// C++ equivalent: `NS::String* label() const`
35    pub fn label(&self) -> Option<String> {
36        unsafe {
37            let ptr: *mut c_void = msg_send_0(self.as_ptr(), sel!(label));
38            if ptr.is_null() {
39                return None;
40            }
41            let utf8_ptr: *const std::ffi::c_char =
42                mtl_sys::msg_send_0(ptr as *const c_void, sel!(UTF8String));
43            if utf8_ptr.is_null() {
44                return None;
45            }
46            let c_str = std::ffi::CStr::from_ptr(utf8_ptr);
47            Some(c_str.to_string_lossy().into_owned())
48        }
49    }
50
51    /// Set the label.
52    ///
53    /// C++ equivalent: `void setLabel(const NS::String*)`
54    pub fn set_label(&self, label: &str) {
55        if let Some(ns_label) = mtl_foundation::String::from_str(label) {
56            unsafe {
57                msg_send_1::<(), *const c_void>(self.as_ptr(), sel!(setLabel:), ns_label.as_ptr());
58            }
59        }
60    }
61}
62
63impl Clone for IOFileHandle {
64    fn clone(&self) -> Self {
65        unsafe {
66            msg_send_0::<*mut c_void>(self.as_ptr(), sel!(retain));
67        }
68        Self(self.0)
69    }
70}
71
72impl Drop for IOFileHandle {
73    fn drop(&mut self) {
74        unsafe {
75            msg_send_0::<()>(self.as_ptr(), sel!(release));
76        }
77    }
78}
79
80impl Referencing for IOFileHandle {
81    #[inline]
82    fn as_ptr(&self) -> *const c_void {
83        self.0.as_ptr()
84    }
85}
86
87unsafe impl Send for IOFileHandle {}
88unsafe impl Sync for IOFileHandle {}