Skip to content

Conversation

jbatez
Copy link
Contributor

@jbatez jbatez commented Jun 7, 2025

Uses the darwin_objc (rust-lang/rust#145496) language feature.

@jbatez jbatez force-pushed the core_ffi_objc branch 3 times, most recently from 68abe05 to 89aebd5 Compare June 7, 2025 22:33
Copy link
Owner

@madsmtm madsmtm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I'm probably going to wait with merging this until there's at least a rust-lang/rust PR open that we can point people towards.

@madsmtm madsmtm added enhancement New feature or request A-objc2 Affects the `objc2`, `objc2-exception-helper` and/or `objc2-encode` crates labels Jun 7, 2025
@jbatez
Copy link
Contributor Author

jbatez commented Jun 8, 2025

I added support for the class! macro as well but just realized extern_class! uses __class_inner! directly. Since extern_class! needs to support #[name = "StringLiteral"], that complicates my core::ffi::objc::class! macro which expects an identifier and not a string literal.

I wonder if my core::ffi::objc macros should just take string literals.

@jbatez
Copy link
Contributor Author

jbatez commented Jun 9, 2025

I made core::ffi::objc::class! accept either an identifier or a string literal and got it working with extern_class!.

@madsmtm
Copy link
Owner

madsmtm commented Jun 10, 2025

Re identifier vs. string literal: I tend to think the core::ffi::objc::* macros might actually want to only accept string literals for simplicity? Because to the runtime, that is what they are, it is only incidental that Objective-C prevents you from having weird things in them like spaces.

They aren't even limited to UTF-8, so might actually make sense to force c-string literals, to prevent interior NUL bytes by construction?

@jbatez
Copy link
Contributor Author

jbatez commented Jun 10, 2025

I'd be ok with string literals only. C-string literals only would make extern_class! #[name = "StringLiteral"] difficult, though, right? I don't know how I'd do that without procedural macros.

@jbatez
Copy link
Contributor Author

jbatez commented Jun 10, 2025

#[export_name = ""] and #[link_name = ""] have similar design constraints, fwiw. They disallow interior NUL bytes and I imagine there are platforms out there that support non-Unicode symbols that Rust has no way of handling.

EDIT: Only export_name disallows nulls, but I think the point still stands.

@madsmtm
Copy link
Owner

madsmtm commented Jun 10, 2025

Both good points, then a normal string literal is probably fine.

@jbatez
Copy link
Contributor Author

jbatez commented Jun 10, 2025

Done. They now expect string literals and are used in new implementations of __class_inner! and __sel_inner!. Since core::ffi::objc::selector! doesn't expect a null-terminator, I added a new __sel_data_terminate! macro that conditionally null-terminates. It's used in __sel_data! and in common_selectors.rs.

Though now I'm wondering if I should change __sel_inner to never expect null-terminated data and instead have it decide whether or not to add a null-terminator.

@madsmtm madsmtm added this to the objc2 v0.7.0 milestone Aug 17, 2025
@jbatez
Copy link
Contributor Author

jbatez commented Aug 21, 2025

The rustc feature plan has changed slightly. See rust-lang/rust#145496. I'll revisit this PR once we make more progress on the rustc changes.

@jbatez jbatez force-pushed the core_ffi_objc branch 2 times, most recently from 619c1a4 to 043ae79 Compare August 23, 2025 17:40
@jbatez jbatez force-pushed the core_ffi_objc branch 2 times, most recently from a2990b0 to 5534291 Compare September 20, 2025 15:37
@jbatez jbatez changed the title add unstable-core-ffi-objc feature add unstable-core-darwin-objc feature Sep 20, 2025
@jbatez
Copy link
Contributor Author

jbatez commented Sep 20, 2025

Updated now that darwin_objc is in nightly.

I tested with examples/metal and am about to add it as an optional feature to the other examples.

@jbatez
Copy link
Contributor Author

jbatez commented Sep 24, 2025

@madsmtm is there anything else you'd like to see before this gets merged?

Copy link
Owner

@madsmtm madsmtm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only have a few smaller things, am otherwise happy with this as-is.

If you don't get to it within the next few days, I'll probably fix it myself, I'd like to get the framework crate parts out with v0.3.2.

path = "speech_synthesis.rs"

[features]
unstable-core-darwin-objc = []
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: The examples actually can use:

Suggested change
unstable-core-darwin-objc = []
unstable-core-darwin-objc = ["objc2/unstable-core-darwin-objc", "objc2-foundation/unstable-core-darwin-objc"]

Because we don't ship them anywhere.

Copy link
Contributor Author

@jbatez jbatez Sep 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left them out because running cargo run --example <name here> --features unstable-core-darwin-objc seemed to enable it in all the dependencies implicitly. I'm no cargo expert, though, so I defer to you on this.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, but it doesn't happen if you do cargo run -pexamples-audio --example speech_synthesis --features unstable-core-darwin-objc (or you are cd-ed into examples/audio). I went and added it.

#
# Please test it, and report any issues you may find:
# https://github.com/madsmtm/objc2/issues/new
unstable-core-darwin-objc = []
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: I'd prefer this was named something more descriptive, maybe unstable-static-via-rustc, unstable-static-nightly or unstable-static-proper? At the very least, it should have static in the name.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively, we make it so that unstable-core-darwin-objc augments unstable-static-sel/unstable-static-class (so you need to enable all three).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer the augmentation approach since in most of the crates all this feature does is enable the darwin_objc language feature. Maybe unstable-rustc-darwin-objc is a better name?

I'm a little busy this weekend so feel free to take over if you want it sooner.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I renamed it to just unstable-darwin-objc (since that is the name of the feature in the compiler), and went with the augmentation route.

jbatez and others added 3 commits September 29, 2025 19:35
There isn't stability issues with this, since we don't ship the
examples. And this more clearly shows what users would want to do.
@madsmtm
Copy link
Owner

madsmtm commented Sep 29, 2025

I went and implemented the things I suggested myself, thanks again for your work here!

@madsmtm madsmtm merged commit 9bd26ee into madsmtm:main Sep 29, 2025
18 checks passed
@jbatez
Copy link
Contributor Author

jbatez commented Sep 30, 2025

No problem, thanks for all your help!

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 this pull request may close these issues.

2 participants