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

Add extern_methods! macro #217

Merged
merged 3 commits into from
Aug 14, 2022
Merged

Add extern_methods! macro #217

merged 3 commits into from
Aug 14, 2022

Conversation

madsmtm
Copy link
Owner

@madsmtm madsmtm commented Jul 24, 2022

Replaces #161.

Syntax:

extern_methods!(
    // `unsafe` here signifies that 
    unsafe impl NSData {
        #[sel_id(new)] // TODO: Doesn't work yet!
        pub fn new() -> Id<Self, Shared>;

        #[sel(length)]
        pub fn len(&self) -> usize;

        // This has a body, and hence is not touched at all
        pub fn is_empty(&self) -> bool {
            self.len() == 0
        }

        #[sel(bytes)]
        fn bytes_raw(&self) -> *const c_void;
    }
);

// Becomes:

impl NSData {
    pub fn new() -> Id<Self, Shared> {
        unsafe { msg_send_id![Self::class(), new].expect("unexpected NULL NSData") }
    }

    pub fn len(&self) -> usize {
        unsafe { msg_send![self, length] }
    }

    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    #[sel(bytes)]
    fn bytes_raw(&self) -> *const c_void;
}

TODO:

  • Documentation
  • Changelog
  • Find a way to allow using msg_send_id and msg_send_bool - should be done in a way that declare_class! also supports such things!
    • Postponed. See Proper bool handling #239 for the msg_send_bool situation. Will be making a #[sel_id(...)] attribute, but that is much easier to do with this merged

@madsmtm madsmtm added enhancement New feature or request A-framework Affects the framework crates and the translator for them labels Jul 24, 2022
@madsmtm
Copy link
Owner Author

madsmtm commented Jul 24, 2022

I've explicitly used it as:

extern_methods!(
    ...
);

Since that allows rustfmt to format the macro contents. Will document this as well! Documented on extern_class! macro.

@madsmtm madsmtm force-pushed the extern-methods-macro branch 4 times, most recently from c73de13 to df5ba6b Compare August 1, 2022 18:02
@madsmtm madsmtm mentioned this pull request Aug 1, 2022
3 tasks
@madsmtm
Copy link
Owner Author

madsmtm commented Aug 1, 2022

I'm a bit unsure of how big of an improvement this really is. While it is more succinct, error messages get a lot worse, go to source doesn't work in docs, and the unsafety is kind of hidden away.

I did find a bug by converting things to use this: NSArray::objects_in_range was using Vec::as_ptr when it should have been using Vec::as_mut_ptr. So that's good, I guess!

EDIT: With #239 this becomes much more useful, and there's a nice symmetry between this and declare_class, so I think it has value

madsmtm added 2 commits August 9, 2022 16:01
Just a simple manual wrapping; we don't use the macro's functionality yet!
@madsmtm madsmtm force-pushed the extern-methods-macro branch 2 times, most recently from 65e38bb to 7945310 Compare August 9, 2022 14:14
@madsmtm madsmtm mentioned this pull request Aug 13, 2022
5 tasks
@madsmtm madsmtm force-pushed the extern-methods-macro branch from 7945310 to b86c94b Compare August 14, 2022 12:14
@madsmtm madsmtm force-pushed the extern-methods-macro branch from b86c94b to 3f1e53f Compare August 14, 2022 12:22
@madsmtm madsmtm merged commit 0c231f6 into master Aug 14, 2022
@madsmtm madsmtm deleted the extern-methods-macro branch August 14, 2022 19:05
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

Successfully merging this pull request may close these issues.

1 participant