Rantanen / intercom

Object based cross-language FFI for Rust
MIT License
63 stars 7 forks source link

Implement ExternType to back ExternInput/Output #178

Closed Rantanen closed 3 years ago

Rantanen commented 3 years ago

The main goal here is to have ExternInput/ExternOutput share the foreign type. So each Rust type can only have one foreign type, which is used for both input and output purposes.

This does result in some curiosities, such as "input strings" being *mut c_char, despite the callee not being allowed to modify them. Essentially the constant-ness of types is removed from the type system, instead the ForeignType is now intended to convey the ABI types, which should be compatible between input/output to allow reusing (future) compound types.


Some history!

At the start none of the type system was codified in traits. This was changed in one of the largest Intercom changes during 2018-2019 when the ExternType trait was added in MR #119. This trait allowed specifying input/output types in the Rust type system, which simplified a lot of the generated code as it could just use Foo::ExternOutputType instead of having to resolve the concrete type at (proc macro) runtime.

The initial implementation just implemented conversions between various types and had it all work through (unsafe) From/Into calls. Later it was discovered that the type conversions really wanted to know whether the types were coming to Rust (and thus would be managed by Rust's memory model) or if they were being sent out of Rust (and thus their memory wouldn't be managed anymore).

This resulted in splitting ExternType into the current ExternInput and ExternOutput in #141. The new traits included the conversion implmementation per-type instead of just relying on generic Into/From implementation.

And finally we're back to adding ExternType trait again. Now for a different purpose though. We till have ExternInput and ExternOutput that define how the type conversions are done, but both of these traits now require ExternType to be implemented as the ExternType defines a single associated type to be used as the COM foreign type independent of whether the value is being used as input or output value.