use objc2::{
extern_class, extern_conformance, extern_methods, msg_send,
rc::{Allocated, Retained},
};
use objc2_foundation::{CopyingHelper, NSArray, NSCopying, NSObject, NSObjectProtocol};
use crate::{MTL4FunctionDescriptor, MTLFunctionStitchingGraph};
extern_class!(
#[unsafe(super(MTL4FunctionDescriptor, NSObject))]
#[derive(Debug, PartialEq, Eq, Hash)]
pub struct MTL4StitchedFunctionDescriptor;
);
extern_conformance!(
unsafe impl NSCopying for MTL4StitchedFunctionDescriptor {}
);
unsafe impl CopyingHelper for MTL4StitchedFunctionDescriptor {
type Result = Self;
}
extern_conformance!(
unsafe impl NSObjectProtocol for MTL4StitchedFunctionDescriptor {}
);
impl MTL4StitchedFunctionDescriptor {
extern_methods!(
#[unsafe(method(functionGraph))]
#[unsafe(method_family = none)]
pub fn function_graph(&self) -> Option<Retained<MTLFunctionStitchingGraph>>;
#[unsafe(method(setFunctionGraph:))]
#[unsafe(method_family = none)]
pub fn set_function_graph(
&self,
function_graph: Option<&MTLFunctionStitchingGraph>,
);
);
pub fn function_descriptors(&self) -> Option<Box<[Retained<MTL4FunctionDescriptor>]>> {
let descriptors: Option<Retained<NSArray<MTL4FunctionDescriptor>>> =
unsafe { msg_send![self, functionDescriptors] };
descriptors.map(|descriptors| descriptors.to_vec().into_boxed_slice())
}
pub fn set_function_descriptors(
&self,
function_descriptors: Option<&[&MTL4FunctionDescriptor]>,
) {
let function_descriptors = function_descriptors.map(NSArray::from_slice);
unsafe {
let _: () = msg_send![self, setFunctionDescriptors: function_descriptors.as_deref()];
}
}
}
impl MTL4StitchedFunctionDescriptor {
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>;
);
}
#[cfg(test)]
mod tests {
use objc2::rc::Retained;
use super::{MTL4FunctionDescriptor, MTL4StitchedFunctionDescriptor};
#[test]
fn collection_method_has_rust_native_signature() {
let _: fn(&MTL4StitchedFunctionDescriptor) -> Option<Box<[Retained<MTL4FunctionDescriptor>]>> =
MTL4StitchedFunctionDescriptor::function_descriptors;
let _: fn(&MTL4StitchedFunctionDescriptor, Option<&[&MTL4FunctionDescriptor]>) =
MTL4StitchedFunctionDescriptor::set_function_descriptors;
}
}