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

Use &CStr instead of &str in runtime functions #564

Closed
madsmtm opened this issue Jan 18, 2024 · 0 comments
Closed

Use &CStr instead of &str in runtime functions #564

madsmtm opened this issue Jan 18, 2024 · 0 comments
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 Jan 18, 2024

Objective-C runtimes use C strings, which we convert to and from &str in objc2::runtime. This unfortunately makes our interop not zero-cost, as we often have to allocate to get an extra zero-byte in the string.

Instead, we should expose the methods with a type of &CStr. This will make those functions accept and return non-UTF-8 strings though, but I think that is also strictly the correct thing to do? Usually, the strings will be C identifiers, which are a restricted subset of ASCII, and Swift's Unicode identifiers are encoded as UTF-8, but I don't think that's required as part of the contract? I.e. I'm allowed to make classes with a non-UTF-8 name, and that's sound, it'll just be very unexpected.

I'm not going to do this before c"..." string literatals are stable though, as the ergonomics are considerably worse without that.

@madsmtm madsmtm added enhancement New feature or request A-objc2 Affects the `objc2`, `objc2-exception-helper` and/or `objc2-encode` crates labels Jan 18, 2024
@madsmtm madsmtm added this to the objc2 v0.6 milestone May 20, 2024
madsmtm added a commit that referenced this issue Sep 17, 2024
We have effectively no control over neither class names, selector names,
protocol names and so on, and external code _could_ theoretically set
these to non-UTF-8 values (although unlikely). So panicking when reading
them is not a good idea.

Furthermore, using CStr avoids allocating when using the runtime, which
is a really nice property.

Fixes #564.
madsmtm added a commit that referenced this issue Sep 17, 2024
We have effectively no control over neither class names, selector names,
protocol names and so on, and external code _could_ theoretically set
these to non-UTF-8 values (although unlikely). So panicking when reading
them is not a good idea.

Furthermore, using CStr avoids allocating when using the runtime, which
is a really nice property.

Fixes #564.
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

No branches or pull requests

1 participant