uniffi-rs icon indicating copy to clipboard operation
uniffi-rs copied to clipboard

WIP give `ComponentInterface` members a pointer to their parent.

Open rfk opened this issue 4 years ago • 0 comments

Here's something I wanted to toy with given recent work on refactoring our code-generation templates, and the desire for each interface member to be able to render itself. The issue is that sometimes we need to know information about the surrounding interface or its types in order to do the code generation, which shows up in awkward calls like:

{% if e.contains_object_references(ci) %}

What would it take to make this be just a plain method call without extra global context? Like so:

{% if e.contains_object_references() %}

In a garbage-collected language, we might conveniently give the Record, Enum etc objects a reference to their containing ComponentInterface and be done with it. That's hard to do in the obvious way in Rust, and the usual workarounds like handing out an Arc<RefCell<ComponentInterface>> seem extremely verbose/complex for our needs.

Here's a sketch of something that might work, although I'm not convinced of the value/complexity tradeoff.

Taking the Enum struct as an example, I've split it up into two parts:

  • EnumDescr is the data that describes an enum. Instances of this are owned by a ComponentInterface in a normal tree structure without backwards references.
  • Enum is a kind of aggregate transient pointer, combining a reference to an EnumDescr with a reference to its containing ComponentInterface. Instances of this are synthesized on demand when inspecting a ComponentInterface.

From the consumer's perspective, it calls ci.get_enum_definition("MyEnum") and receives an Enum struct, and it can call methods on the Enum struct to inspect its definition. But these methods all delegate to the underlying EnumDescr for the actual data.

It gets a little complicated in the details of nested strcuts like Variant and Field, and I'll leave some musings inline.

Thoughts?

rfk avatar Aug 05 '21 10:08 rfk