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

derive macros #55

Open
madsmtm opened this issue Oct 30, 2021 · 4 comments
Open

derive macros #55

madsmtm opened this issue Oct 30, 2021 · 4 comments
Labels
A-objc2 Affects the `objc2`, `objc2-exception-helper` and/or `objc2-encode` crates documentation Improvements or additions to documentation

Comments

@madsmtm
Copy link
Owner

madsmtm commented Oct 30, 2021

We have a few traits that users has to implement manually, namely Encode, RefEncode, Message and the INS... traits. It would be nice if these could be automatically derived, but in essence we can't allow that because they are unsafe, and a derive macro wouldn't be able to uphold the required safety invariants.

#[derive(Encode)] (requested upstream in SSheldon/rust-objc#52) specifically doesn't work because the user could create a #[repr(C)] struct which used e.g. [i8; 4] while the Objective-C side used int. Or *const u8 vs. c_void*; this is valid from a normal FFI-perspective, but Encode requires that the encodings match perfectly.

So I'm actually probably going to remove the existing objc2_foundation_derive helpers in this repo.

@madsmtm madsmtm added the documentation Improvements or additions to documentation label Oct 30, 2021
@madsmtm madsmtm mentioned this issue Oct 30, 2021
80 tasks
@madsmtm
Copy link
Owner Author

madsmtm commented Oct 30, 2021

And my personal take: I really dislike the extra compilation cost and the complexity in implementing them; I'd any day rather use macro_rules!

madsmtm added a commit that referenced this issue Oct 30, 2021
@madsmtm
Copy link
Owner Author

madsmtm commented Oct 30, 2021

objc2_foundation_derive is gone. I'll close this issue, but if someone wants to argue in favor of derive macros, feel free!

@madsmtm madsmtm closed this as completed Oct 30, 2021
@madsmtm
Copy link
Owner Author

madsmtm commented Dec 3, 2021

a derive macro wouldn't be able to uphold the required safety invariants.

Or *const u8 vs. c_void*; this is valid from a normal FFI-perspective, but Encode requires that the encodings match perfectly.

I'm finding, when converting huge amounts of code, that derived Encode and RefEncode might actually be useful... Maybe we can relax the safety requirements in them so that they don't have to be implemented correctly, only that they have to be correct FFI-wise? Or something...
Will have to match the objc_msgSend functions in message/apple/...!

@madsmtm madsmtm reopened this Dec 10, 2021
@madsmtm
Copy link
Owner Author

madsmtm commented Jan 6, 2022

We could maybe also add a "old-compat" feature flag, which also implements Encode from the objc v0.2.7 crate; then crates from e.g. core-foundation-rs wouldn't need to choose between objc and objc2 support:

unsafe impl objc::Encode for T {
    #[inline]
    fn encode() -> objc::Encoding {
        objc::Encoding::from_str(T::ENCODING)
    }
}

@madsmtm madsmtm changed the title My take on derive macros derive macros Apr 21, 2023
@madsmtm madsmtm added the A-objc2 Affects the `objc2`, `objc2-exception-helper` and/or `objc2-encode` crates label May 24, 2024
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 documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

1 participant