wit-bindgen icon indicating copy to clipboard operation
wit-bindgen copied to clipboard

Suggest: generate `drop`, `new` and `rep` for aliased handle types

Open Mossaka opened this issue 2 years ago • 1 comments

In tests/codegen/resource-alias.wit, we generate handle types for each alias.

typedef exports_my_resources_e1_own_x_t exports_my_resources_e2_own_x_t;

typedef exports_my_resources_e1_borrow_x_t exports_my_resources_e2_borrow_x_t;

However, we only generate helper functions for e1, not for e2.

extern void exports_my_resources_e1_x_drop_own(exports_my_resources_e1_own_x_t handle);
extern void exports_my_resources_e1_x_drop_borrow(exports_my_resources_e1_own_x_t handle);
extern exports_my_resources_e1_own_x_t exports_my_resources_e1_x_new(exports_my_resources_e1_x_t *rep);
extern exports_my_resources_e1_x_t* exports_my_resources_e1_x_rep(exports_my_resources_e1_own_x_t handle);
void exports_my_resources_e1_x_destructor(exports_my_resources_e1_x_t *rep);

If feels pretty strange from user perspectives that even though we are working inside interface e2, but we have to call functions with interface name e1 to create own and borrow handle types.

Suggestion: we generate the same set of helper functions for aliased handle types.

Thoughts? @alexcrichton @dicej

Mossaka avatar Nov 22 '23 00:11 Mossaka

On one hand this matches WIT where when a type is use-d between interfaces it defines the name in the new interface. On the other hand this is not the easiest to work with in C becausey ou always have to remember the original name. That being said not much is easy to work with in C anyway and the typedefs for use-d names don't have to be used an instead e1 could be used everywhere for example.

I'd personally have a slight preference and being conservative and not providing multiple names for the intrinsics.

alexcrichton avatar Nov 22 '23 16:11 alexcrichton