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?
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 { ... }
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?
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