When using cargo component, if there are two functions imported, and the return type of one import corresponds to the param type of the other, is there by chance a nice way to convert a value from the wit return type to the wit param type so that you can feed the output of one import into the other?
Or I guess what I'm really wondering is if it would be useful for the generated result types and param types in the bindings to implement From
traits
Right now, no, as the generated bindings don't know they're the same type (I assume we're not talking primitives)
This will be solved in the future with improved id / version information in the extern descriptions plus registry support
Oh unless I misread the question and this is all from the same wit package
Then that's a wit-bindgen issue where the types aren't being shared correctly
Perhaps a concrete example might clear up my confusion
It is all from one wit package
let checkpoint = bindings::storage::imp_get_checkpoint();
let root = bindings::storage::hash_checkpoint(checkpoint);
Both functions are wit imports in the same package, but get_checkpoint returns a CheckpointReturn
type and hash_checkpoint
receives a CheckpointParam
type
so the two lines I have won't work, but if I could do hash_checkpoint(CheckpointParam::from(checkpoint))
then this would work
let checkpoint = bindings::storage::imp_get_checkpoint();
let root = bindings::storage::hash_checkpoint(bindings::storage::CheckpointParam {
contents: bindings::storage::ContentParam {
log_root: &checkpoint.contents.log_root,
log_length: checkpoint.contents.log_length,
map_root: &checkpoint.contents.map_root
},
key_id: &checkpoint.key_id,
signature: &checkpoint.signature
});
In this example, as a dev, I can do this, where I create a CheckpointParam
myself based on the CheckpointResult
that I have, but in general folks might have more complicated structs, and a desire to convert between the two types using a trait implemented in the bindings themselves...
Essentially a From
implementation that does what I just did above being included in the bindings
In retrospect I see that this would have made more sense in the wit-bindgen
stream
Oh I see, the difference between generated param / result types for borrowing purposes, yeah
Indeed, some conversions for those make sense
and wit-bindgen would be the place
Would it make sense to use a single type with Cow
s?
Posted a link to this topic over in wit-bindgen
Last updated: Nov 22 2024 at 16:03 UTC