Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert ptr, count arguments into slice pointers #610

Open
madsmtm opened this issue May 1, 2024 · 0 comments
Open

Convert ptr, count arguments into slice pointers #610

madsmtm opened this issue May 1, 2024 · 0 comments
Labels
A-framework Affects the framework crates and the translator for them enhancement New feature or request

Comments

@madsmtm
Copy link
Owner

madsmtm commented May 1, 2024

Convert things like:

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

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

#[method_id(managedObjectIDFromUTF8String:length:)]
pub unsafe fn managedObjectIDFromUTF8String_length(
    &self,
    utf8string: NonNull<c_char>,
    len: NSUInteger,
) -> Retained<NSManagedObjectID>;

Into:

pub unsafe fn initWithObjects(
    this: Allocated<Self>,
    objects: *mut [NonNull<ObjectType>],
) -> Retained<Self> {
    let cnt = objects.len();
    let objects = objects.as_mut_ptr();
    unsafe { msg_send_id![this, initWithObjects: objects, count: cnt] }
}

pub unsafe fn colorWithColorSpace_components(
    space: &NSColorSpace,
    components: NonNull<[CGFloat]>,
) -> Retained<NSColor> {
    let number_of_components = components.len();
    let components = components.as_non_null_ptr();
    unsafe { msg_send_id![NSColor::class(), colorWithColorSpace: objects, components: components, count: number_of_components] }
}

pub unsafe fn managedObjectIDFromUTF8String(
    &self,
    utf8string: NonNull<[c_char]>,
) -> Retained<NSManagedObjectID> {
    let len = utf8string.len();
    let utf8string = utf8string.as_non_null_ptr();
    unsafe { msg_send_id![self, managedObjectIDFromUTF8String: utf8string, length: len] }
}

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?

@madsmtm madsmtm added enhancement New feature or request A-framework Affects the framework crates and the translator for them labels May 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-framework Affects the framework crates and the translator for them enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant