mtl_gpu/device/
binary_archive.rs1use std::ffi::c_void;
6
7use mtl_foundation::Referencing;
8use mtl_sys::{msg_send_2, sel};
9
10use super::Device;
11use crate::binary_archive::{BinaryArchive, BinaryArchiveDescriptor};
12
13impl Device {
14 pub fn new_binary_archive(
18 &self,
19 descriptor: &BinaryArchiveDescriptor,
20 ) -> Result<BinaryArchive, mtl_foundation::Error> {
21 unsafe {
22 let mut error: *mut c_void = std::ptr::null_mut();
23 let ptr: *mut c_void = msg_send_2(
24 self.as_ptr(),
25 sel!(newBinaryArchiveWithDescriptor:error:),
26 descriptor.as_ptr(),
27 &mut error as *mut _,
28 );
29
30 if ptr.is_null() {
31 if !error.is_null() {
32 return Err(
33 mtl_foundation::Error::from_ptr(error).expect("error should be valid")
34 );
35 }
36 return Err(mtl_foundation::Error::error(
37 std::ptr::null_mut(),
38 -1,
39 std::ptr::null_mut(),
40 )
41 .expect("failed to create error"));
42 }
43
44 Ok(BinaryArchive::from_raw(ptr).expect("failed to create binary archive"))
45 }
46 }
47}
48
49#[cfg(test)]
50mod tests {
51 use super::*;
52 use crate::device::system_default;
53
54 #[test]
55 fn test_new_binary_archive() {
56 let device = system_default().expect("no Metal device");
57 let descriptor = BinaryArchiveDescriptor::new().expect("failed to create descriptor");
58
59 let result = device.new_binary_archive(&descriptor);
61 assert!(result.is_ok());
62 }
63}