mtl-rs 0.1.10

Rust bindings for Apple's Metal API
use objc2::{
    extern_class, extern_conformance, extern_methods, msg_send,
    rc::{Allocated, Retained},
    runtime::ProtocolObject,
};
use objc2_foundation::{CopyingHelper, NSCopying, NSObject, NSObjectProtocol, NSString};

use crate::*;

extern_class!(
    /// Describes a shader function from a Metal library.
    ///
    /// See also [Apple's documentation](https://developer.apple.com/documentation/metal/mtl4libraryfunctiondescriptor?language=objc)
    #[unsafe(super(MTL4FunctionDescriptor, NSObject))]
    #[derive(Debug, PartialEq, Eq, Hash)]
    pub struct MTL4LibraryFunctionDescriptor;
);

extern_conformance!(
    unsafe impl NSCopying for MTL4LibraryFunctionDescriptor {}
);

unsafe impl CopyingHelper for MTL4LibraryFunctionDescriptor {
    type Result = Self;
}

extern_conformance!(
    unsafe impl NSObjectProtocol for MTL4LibraryFunctionDescriptor {}
);

impl MTL4LibraryFunctionDescriptor {
    extern_methods!(
        /// Returns a reference to the library containing the function.
        #[unsafe(method(library))]
        #[unsafe(method_family = none)]
        pub fn library(&self) -> Option<Retained<ProtocolObject<dyn MTLLibrary>>>;

        /// Setter for [`library`][Self::library].
        #[unsafe(method(setLibrary:))]
        #[unsafe(method_family = none)]
        pub fn set_library(
            &self,
            library: Option<&ProtocolObject<dyn MTLLibrary>>,
        );
    );

    pub fn name(&self) -> Option<String> {
        let name: Option<Retained<NSString>> = unsafe { msg_send![self, name] };
        name.map(|value| value.to_string())
    }

    pub fn set_name(
        &self,
        name: Option<&str>,
    ) {
        unsafe {
            let _: () = msg_send![self, setName: name.map(NSString::from_str).as_deref()];
        }
    }
}

/// Methods declared on superclass `NSObject`.
impl MTL4LibraryFunctionDescriptor {
    extern_methods!(
        #[unsafe(method(init))]
        #[unsafe(method_family = init)]
        pub fn init(this: Allocated<Self>) -> Retained<Self>;

        #[unsafe(method(new))]
        #[unsafe(method_family = new)]
        pub fn new() -> Retained<Self>;
    );
}