mtl-rs 0.1.10

Rust bindings for Apple's Metal API
use objc2::{Message, extern_protocol, msg_send, rc::Retained};
use objc2_foundation::{NSObjectProtocol, NSString};

use crate::*;

extern_protocol!(
    /// Represents a binary function.
    ///
    /// A binary function is a shader that you precompile from Metal IR to GPU machine code.
    ///
    /// See also [Apple's documentation](https://developer.apple.com/documentation/metal/mtl4binaryfunction?language=objc)
    pub unsafe trait MTL4BinaryFunction: NSObjectProtocol + Send + Sync {
        /// Describes the type of this binary function.
        #[unsafe(method(functionType))]
        #[unsafe(method_family = none)]
        fn function_type(&self) -> MTLFunctionType;
    }
);

pub trait MTL4BinaryFunctionExt: MTL4BinaryFunction + Message {
    /// Obtains the optional name of this binary function.
    fn name(&self) -> Option<String> {
        let s: Option<Retained<NSString>> = unsafe { msg_send![self, name] };
        s.map(|v| v.to_string())
    }
}

impl<T: MTL4BinaryFunction + Message> MTL4BinaryFunctionExt for T {}