mtl_gpu/io/
scratch_buffer.rs1use std::ffi::c_void;
4use std::ptr::NonNull;
5
6use mtl_foundation::Referencing;
7use mtl_sys::{msg_send_0, sel};
8
9use crate::buffer::Buffer;
10
11#[repr(transparent)]
15pub struct IOScratchBuffer(pub(crate) NonNull<c_void>);
16
17impl IOScratchBuffer {
18 #[inline]
24 pub unsafe fn from_raw(ptr: *mut c_void) -> Option<Self> {
25 NonNull::new(ptr).map(Self)
26 }
27
28 #[inline]
30 pub fn as_raw(&self) -> *mut c_void {
31 self.0.as_ptr()
32 }
33
34 pub fn buffer(&self) -> Option<Buffer> {
38 unsafe {
39 let ptr: *mut c_void = msg_send_0(self.as_ptr(), sel!(buffer));
40 if ptr.is_null() {
41 return None;
42 }
43 let _: *mut c_void = msg_send_0(ptr, sel!(retain));
44 Buffer::from_raw(ptr)
45 }
46 }
47}
48
49impl Clone for IOScratchBuffer {
50 fn clone(&self) -> Self {
51 unsafe {
52 msg_send_0::<*mut c_void>(self.as_ptr(), sel!(retain));
53 }
54 Self(self.0)
55 }
56}
57
58impl Drop for IOScratchBuffer {
59 fn drop(&mut self) {
60 unsafe {
61 msg_send_0::<()>(self.as_ptr(), sel!(release));
62 }
63 }
64}
65
66impl Referencing for IOScratchBuffer {
67 #[inline]
68 fn as_ptr(&self) -> *const c_void {
69 self.0.as_ptr()
70 }
71}
72
73unsafe impl Send for IOScratchBuffer {}
74unsafe impl Sync for IOScratchBuffer {}