Skip to content

Conversation

@madsmtm
Copy link
Owner

@madsmtm madsmtm commented Oct 3, 2023

Using a combination of what's described here and here, now we have:

#[derive(Debug, PartialEq, Eq, Hash, Clone)]
struct MyState {
    my_variable: CanBeAnything,
    // ...
}

declare_class!(
    struct MyClass;

    unsafe impl ClassType for MyClass {
        type Super = NSObject;
        type Mutability = InteriorMutable;
        const NAME: &'static str = "MyClass";
    }

    impl DeclaredClass for MyClass {
        type Ivars = MyState; // Or even `RefCell<MyState>`, depending on if you want fine-grained or coarse locking
    }

    unsafe impl MyClass {
        #[method_id(init)]
        fn init(this: Allocated<Self>) -> Option<Id<Self>> {
            let this = this.set_ivars(MyState {
                my_variable: Default::default(),
                // ...
            });
            unsafe { msg_send_id![super(this), init] }
        }

        #[method(doStuff)]
        fn do_stuff(&self) {
            self.ivars().my_variable.do_stuff();
        }
    }
);

Fixes #414.
Fixes #438.
Fixes #253.
Fixes #513.
Fixes #458.
Replaces / fixes #288.
Replaces / fixes #411.
Replaces / fixes #173.

TODO:

  • Make it work fully.
  • Documentation.
  • More thorough tests.
  • Go through all the linked issues, and ensure all items are completed.
  • Consider also allowing msg_send_id![this, init] where this: PartialInit in addition to msg_send_id![super(this), init] (since the specific initializer may not have been overridden)
    • Punting this. We'll rather want some sort of special init handling for things that inherit NSObject?

@madsmtm madsmtm added enhancement New feature or request A-objc2 Affects the `objc2`, `objc2-exception-helper` and/or `objc2-encode` crates labels Oct 3, 2023
@madsmtm madsmtm added this to the Usable icrate milestone Oct 3, 2023
This was referenced Oct 3, 2023
@madsmtm madsmtm changed the base branch from master to redo-ivars-partial October 3, 2023 14:40
@madsmtm madsmtm force-pushed the redo-ivars-partial branch from a4b8ff8 to 44e8bf8 Compare October 3, 2023 14:43
Base automatically changed from redo-ivars-partial to master October 3, 2023 14:52
@madsmtm madsmtm force-pushed the redo-ivars branch 5 times, most recently from c8a2ed2 to 42dac6d Compare October 5, 2023 23:13
@madsmtm madsmtm marked this pull request as ready for review November 27, 2023 23:45
@madsmtm madsmtm force-pushed the redo-ivars branch 11 times, most recently from 33b3ea6 to 5f19f83 Compare December 2, 2023 21:37
@madsmtm madsmtm force-pushed the redo-ivars branch 7 times, most recently from 9072f42 to 3d50493 Compare December 3, 2023 00:49
At the same time, add support for `msg_send_id![super(...)]`.

Also relax `T: IsMutable` bound on `Allocated::as_mut_ptr`.
Mostly to make the assembly tests more stable
@madsmtm madsmtm merged commit 4e64c1a into master Dec 3, 2023
@madsmtm madsmtm deleted the redo-ivars branch December 3, 2023 03:28
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

1 participant