mtl-rs 0.1.11

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

use crate::MTLFunctionStitchingFunctionNode;

extern_class!(
    /// Function graph describing a DAG used to produce a stitched function.
    ///
    /// Availability: macOS 12.0+, iOS 15.0+
    #[unsafe(super(NSObject))]
    #[derive(Debug, PartialEq, Eq, Hash)]
    pub struct MTLFunctionStitchingGraph;
);

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

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

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

impl MTLFunctionStitchingGraph {
    extern_methods!(
        #[unsafe(method(outputNode))]
        #[unsafe(method_family = none)]
        pub fn output_node(&self) -> Option<Retained<MTLFunctionStitchingFunctionNode>>;

        /// Setter for [`output_node`][Self::output_node].
        #[unsafe(method(setOutputNode:))]
        #[unsafe(method_family = none)]
        pub fn set_output_node(
            &self,
            output_node: Option<&MTLFunctionStitchingFunctionNode>,
        );
    );

    pub fn nodes(&self) -> Box<[Retained<MTLFunctionStitchingFunctionNode>]> {
        let nodes: Retained<NSArray<MTLFunctionStitchingFunctionNode>> = unsafe { msg_send![self, nodes] };
        nodes.to_vec().into_boxed_slice()
    }

    pub fn set_nodes(
        &self,
        nodes: &[&MTLFunctionStitchingFunctionNode],
    ) {
        let nodes = NSArray::from_slice(nodes);
        unsafe {
            let _: () = msg_send![self, setNodes: &*nodes];
        }
    }

    pub fn attributes(&self) -> Box<[Retained<ProtocolObject<dyn super::MTLFunctionStitchingAttribute>>]> {
        let attributes: Retained<NSArray<ProtocolObject<dyn super::MTLFunctionStitchingAttribute>>> =
            unsafe { msg_send![self, attributes] };
        attributes.to_vec().into_boxed_slice()
    }

    pub fn set_attributes(
        &self,
        attributes: &[&ProtocolObject<dyn super::MTLFunctionStitchingAttribute>],
    ) {
        let attributes = NSArray::from_slice(attributes);
        unsafe {
            let _: () = msg_send![self, setAttributes: &*attributes];
        }
    }

    pub fn init_with_function_name_nodes_output_node_attributes(
        this: Allocated<Self>,
        function_name: &str,
        nodes: &[&MTLFunctionStitchingFunctionNode],
        output_node: Option<&MTLFunctionStitchingFunctionNode>,
        attributes: &[&ProtocolObject<dyn super::MTLFunctionStitchingAttribute>],
    ) -> Retained<Self> {
        let function_name = NSString::from_str(function_name);
        let nodes = NSArray::from_slice(nodes);
        let attributes = NSArray::from_slice(attributes);
        unsafe {
            msg_send![
                this,
                initWithFunctionName: &*function_name,
                nodes: &*nodes,
                outputNode: output_node,
                attributes: &*attributes
            ]
        }
    }
}

#[allow(unused)]
impl MTLFunctionStitchingGraph {
    /// The name of the function to call.
    fn function_name(&self) -> String {
        let s: Retained<NSString> = unsafe { msg_send![self, functionName] };
        s.to_string()
    }

    /// Setter for [`function_name`][Self::function_name].
    fn set_function_name(
        &self,
        name: &str,
    ) {
        unsafe {
            let _: () = msg_send![self, setFunctionName: &*NSString::from_str(name)];
        }
    }
}