mtl_gpu/io/
file_handle.rs1use 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#[repr(transparent)]
13pub struct IOFileHandle(pub(crate) NonNull<c_void>);
14
15impl IOFileHandle {
16 #[inline]
22 pub unsafe fn from_raw(ptr: *mut c_void) -> Option<Self> {
23 NonNull::new(ptr).map(Self)
24 }
25
26 #[inline]
28 pub fn as_raw(&self) -> *mut c_void {
29 self.0.as_ptr()
30 }
31
32 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 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 {}