madsmtm / objc2

Bindings to Apple's frameworks in Rust
https://docs.rs/objc2/
MIT License
281 stars 35 forks source link

Convert `ptr, count` arguments into slice pointers #610

Open madsmtm opened 2 weeks ago

madsmtm commented 2 weeks ago

Convert things like:

#[method_id(initWithObjects:count:)]
pub unsafe fn initWithObjects_count(
    this: Allocated<Self>,
    objects: *mut NonNull<ObjectType>,
    cnt: NSUInteger,
) -> Id<Self>;

#[method_id(colorWithColorSpace:components:count:)]
pub unsafe fn colorWithColorSpace_components_count(
    space: &NSColorSpace,
    components: NonNull<CGFloat>,
    number_of_components: NSInteger,
) -> Id<NSColor>;

Into:

pub unsafe fn initWithObjects(
    this: Allocated<Self>,
    objects: *mut [NonNull<ObjectType>],
) -> Id<Self>;

pub unsafe fn colorWithColorSpace_components(
    space: &NSColorSpace,
    components: NonNull<[CGFloat]>,
) -> Id<NSColor>;

That is, methods that have a pointer argument and uses count: with a NSInteger/NSUInteger should instead allow passing a slice pointer, and then we extract the count automatically.

This probably requires that we output a shim for the method manually, as doing this kind of rewriting purely in the type-system is very difficult.

Unsure how this should work in declare_class!? Maybe we do need to integrate it into the type-system? Maybe we need a special attribute that says where the count should go?