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 support for passing empty strings from Kotlin to Rust #756

Merged
merged 2 commits into from
Jan 7, 2025

Conversation

emarteca
Copy link
Member

@emarteca emarteca commented Jan 3, 2025

Previously, we couldn't pass empty strings from Kotlin to Rust. This is because string passing from Kotlin to Rust involves the allocation of a JNA Memory object, which can't be creating with a size of zero, so passing an empty string results in a runtime exception.

In this PR, we add support for passing empty strings from Kotlin to Rust. This is done by casing out Memory allocation so that if the allocated size is zero we actually allocate a size of 1, and then clear it.

Fixes #749

@emarteca emarteca marked this pull request as ready for review January 3, 2025 18:59
@Manishearth
Copy link
Contributor

Hold on, for an empty string can we not just pass down a pointer of value 0x1?

@emarteca
Copy link
Member Author

emarteca commented Jan 3, 2025

Oh hmm, that's true. To do that, I think we'd have to overhaul the current system to have the native (and a few of the read) functions return optional Memory, where it returns null if the length of the slice passed in is zero. And then only close the Memory if it's non-null.
That might be cleaner overall, what do you think?

@Manishearth
Copy link
Contributor

Yes, that makes sense.

@emarteca
Copy link
Member Author

emarteca commented Jan 4, 2025

Done! Good idea, it's much nicer this way 🙏

@emarteca emarteca force-pushed the fix_kotlin_empty_string branch from f9c2f9c to 0af01a8 Compare January 7, 2025 01:56
@emarteca emarteca merged commit 5269118 into rust-diplomat:main Jan 7, 2025
20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support passing empty strings across the boundary from Kotlin --> Rust
2 participants