ralfbiedert / interoptopus

The polyglot bindings generator for your library (C#, C, Python, …) 🐙
302 stars 27 forks source link

Support for passing strings that contain nul accross the FFI boundary #94

Open FeldrinH opened 1 year ago

FeldrinH commented 1 year ago

In C all strings are terminated by a null byte (0x0), which means that strings using the idiomatic C representation cannot contain null. However, many languages, including Python and C# support strings that store length as a separate value and can contain null. It would be nice if Interoptopus provided a canonical way to pass such strings accross the FFI boundary.

The only way to do this currently that I can figure out is to use a slice of bytes and convert manually whenever you cross the FFI boundary, which is incovenient and in for example Python requires copying the entire string for every conversion.

ralfbiedert commented 1 year ago

To comment I'd need more specific info, like how would that work in Python or C#, i.e., what specifically would be a new Rust type, and what would be the foreign counterparts?

FeldrinH commented 1 year ago

what specifically would be a new Rust type, and what would be the foreign counterparts?

I was hoping that the foreign counterpart could be the the target language's built-in string type (e.g. str in Python or string in C#), but that might have technical complications. As for the Rust type I don't really know and as long as I can convert a normal std::string::String into it I don't think it makes much of a difference.