Stream: general

Topic: cargo component param vs result


view this post on Zulip Daniel Macovei (Mar 16 2023 at 16:09):

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?

view this post on Zulip Daniel Macovei (Mar 16 2023 at 16:16):

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

view this post on Zulip Peter Huene (Mar 16 2023 at 16:59):

Right now, no, as the generated bindings don't know they're the same type (I assume we're not talking primitives)

view this post on Zulip Peter Huene (Mar 16 2023 at 17:01):

This will be solved in the future with improved id / version information in the extern descriptions plus registry support

view this post on Zulip Peter Huene (Mar 16 2023 at 17:01):

Oh unless I misread the question and this is all from the same wit package

view this post on Zulip Peter Huene (Mar 16 2023 at 17:02):

Then that's a wit-bindgen issue where the types aren't being shared correctly

view this post on Zulip Peter Huene (Mar 16 2023 at 17:05):

Perhaps a concrete example might clear up my confusion

view this post on Zulip Daniel Macovei (Mar 16 2023 at 17:35):

It is all from one wit package

view this post on Zulip Daniel Macovei (Mar 16 2023 at 17:35):

let checkpoint = bindings::storage::imp_get_checkpoint();
let root = bindings::storage::hash_checkpoint(checkpoint);

view this post on Zulip Daniel Macovei (Mar 16 2023 at 17:36):

Both functions are wit imports in the same package, but get_checkpoint returns a CheckpointReturn type and hash_checkpoint receives a CheckpointParam type

view this post on Zulip Daniel Macovei (Mar 16 2023 at 17:37):

so the two lines I have won't work, but if I could do hash_checkpoint(CheckpointParam::from(checkpoint)) then this would work

view this post on Zulip Daniel Macovei (Mar 16 2023 at 17:46):

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
      });

view this post on Zulip Daniel Macovei (Mar 16 2023 at 17:48):

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...

view this post on Zulip Daniel Macovei (Mar 16 2023 at 17:49):

Essentially a From implementation that does what I just did above being included in the bindings

view this post on Zulip Daniel Macovei (Mar 16 2023 at 17:52):

In retrospect I see that this would have made more sense in the wit-bindgen stream

view this post on Zulip Peter Huene (Mar 16 2023 at 18:17):

Oh I see, the difference between generated param / result types for borrowing purposes, yeah

view this post on Zulip Peter Huene (Mar 16 2023 at 18:17):

Indeed, some conversions for those make sense

view this post on Zulip Peter Huene (Mar 16 2023 at 18:17):

and wit-bindgen would be the place

view this post on Zulip Lann Martin (Mar 16 2023 at 19:02):

Would it make sense to use a single type with Cows?

view this post on Zulip Daniel Macovei (Mar 16 2023 at 19:10):

Posted a link to this topic over in wit-bindgen


Last updated: Nov 22 2024 at 16:03 UTC