Stream: cargo-component

Topic: Share ownership between components?


view this post on Zulip Michael Fuller (Nov 26 2023 at 13:55):

Can we use smart pointers like Arc to share ownership between components?

If I understand correctly, WIT passes types by value by default and has borrow to pass a reference.

How can we have a data type that’s owned by multiple components and dropped when the last one drops it?

view this post on Zulip Joel Dice (Nov 26 2023 at 15:44):

You can give out multiple own handles to a resource that's implemented in terms of an Arc (or, in Rust terms, just an Rc, since we don't have multithreading yet), which has the same effect. For example, with wit-bindgen:

struct ThingThatIWantToShare { ... }
struct Foo(Rc<ThingThatIWantToShare>);
impl foo::bar::baz::Foo for Foo { ... }

view this post on Zulip Michael Fuller (Dec 01 2023 at 11:18):

Great that it just works. I’m curious how does this work under the hood? Is the memory containing the A/Rc value shared across components? Doesn’t every component have its own isolated memory?

view this post on Zulip Till Schneidereit (Dec 01 2023 at 13:38):

the memory backing a resource is still private to one component, or the host. The way this works is that a resource handle comes with a set of functions for operating on it. These functions run inside the component that created the resource, so they can access the backing memory


Last updated: Nov 25 2024 at 19:03 UTC