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

Allow #[method_id] in declare_class! #282

Closed
madsmtm opened this issue Nov 3, 2022 · 1 comment · Fixed by #354
Closed

Allow #[method_id] in declare_class! #282

madsmtm opened this issue Nov 3, 2022 · 1 comment · Fixed by #354
Labels
A-objc2 Affects the `objc2`, `objc2-exception-helper` and/or `objc2-encode` crates enhancement New feature or request

Comments

@madsmtm
Copy link
Owner

madsmtm commented Nov 3, 2022

To make declare_class! and extern_class! closer in functionality (and make using declare_class! much safer in general)!

It'll probably make sense to start supporting super in msg_send_id! as well, see #173.

Example usage (where all of these could also return an Option<Id<_, _>>):

declare_class!(
    struct MyCustomObject;

    unsafe impl ClassType for MyCustomObject {
        type Super = NSObject;
    }

    unsafe impl MyCustomObject {
        #[method_id(newOnClass)]
        fn new() -> Id<Object, Shared> {
            todo!()
        }

        #[method_id(newOnInstance)]
        fn newOnInstance(&self) -> Id<Object, Shared> {
            todo!()
        }

        // Disallowed, since it requires a lot on the user to do correctly
        // #[method_id(alloc)]

        // Always requires `this: Allocated<Self>`
        // Class method `init` that returns an `Id` is disallowed
        #[method_id(init)]
        fn init(this: Allocated<Self>) -> Id<Self, Owned> {
            todo!()
        }

        #[method_id(copyOnClass)]
        fn copyOnClass() -> Id<Object, Shared> {
            todo!()
        }

        #[method_id(copyOnInstance)]
        fn copyOnInstance(&self) -> Id<Object, Shared> {
            todo!()
        }

        #[method_id(otherOnClass)]
        fn otherOnClass() -> Id<Object, Shared> {
            todo!()
        }

        #[method_id(otherOnInstance1)]
        fn otherOnInstance1(&self) -> Id<Object, Shared> {
            todo!()
        }

        #[method_id(otherOnInstance2)]
        fn otherOnInstance2(this: &Self) -> Id<Object, Shared> {
            todo!()
        }
    }
);

And what we should generate:

unsafe impl MyCustomObject {
    fn new(_cls: &Class, _cmd: Sel) -> *mut Object {
        <RT as DeclareClassId>::return_id::<Id<Object, Shared>>({
            todo!()
        }) // Manuallydrop
    }

    fn newOnInstance(&self, _cmd: Sel) -> *mut Object {
        <RT as DeclareClassId>::return_id::<Id<Object, Shared>>(
            todo!()
        }) // Manuallydrop
    }

    fn init(this: *mut Self, _cmd: Sel) -> *mut Self {
        let this = <RT as DeclareClassId>::convert_receiver(this);
        <RT as DeclareClassId>::return_id_this_checked::<Id<Self, Shared>>({
            todo!()
        }) // Manuallydrop
    }

    fn copyOnClass(_cls: &Class, _cmd: Sel) -> *mut Object {
        <RT as DeclareClassId>::return_id::<Id<Object, Shared>>({
            todo!()
        }) // Manuallydrop
    }

    fn copyOnInstance(&self, _cmd: Sel) -> *mut Object {
        <RT as DeclareClassId>::return_id::<Id<Object, Shared>>({
            todo!()
        }) // Manuallydrop
    }

    fn otherOnClass(_cls: &Class, _cmd: Sel) -> *mut Object {
        <RT as DeclareClassId>::return_id::<Id<Object, Shared>>({
            todo!()
        }) // Autorelease
    }

    fn otherOnInstance1(&self, _cmd: Sel) -> *mut Object {
        <RT as DeclareClassId>::return_id::<Id<Object, Shared>>({
            todo!()
        }) // Autorelease
    }

    fn otherOnInstance2(this: &Self, _cmd: Sel) -> *mut Object {
        let this = <RT as DeclareClassId>::convert_receiver(this);
        <RT as DeclareClassId>::return_id_this_checked::<Id<Self, Shared>>({
            todo!()
        }) // Autorelease
    }
}

I'm unsure if we should restrict class method new to always return Id<Self>? Probably not.

@madsmtm madsmtm added enhancement New feature or request A-objc2 Affects the `objc2`, `objc2-exception-helper` and/or `objc2-encode` crates labels Nov 3, 2022
@madsmtm
Copy link
Owner Author

madsmtm commented Dec 31, 2022

Also relevant for protocol conformance, see #317

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-objc2 Affects the `objc2`, `objc2-exception-helper` and/or `objc2-encode` crates enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant