Skip to content

Mutable methods in wit resources #178

Closed

Description

@tqwewe

I'm looking into using resources with the following .wit file:

resource aggregate {
  static new: function(id: string) -> aggregate
  apply-events: function(event: list<list<u8>>)
}

Which generates:

pub trait Aggregate {
    fn new(id: String,) -> wit_bindgen_rust::Handle<super::Aggregate>;
    fn apply_events(&self,event: Vec<Vec<u8>>,);
}

But using the online demo to see the generated rust code for export, the apply-events method takes &self. But what if I want to mutate self with &mut self, is this possible?

Activity

  1. alexcrichton commented on Mar 21, 2022

    @alexcrichton
    Member

    This is currently due to the assumption that components allow for being recursively entered. This means that if Rust handed out &mut T then it would be memory unsafe to recursively call the component. This means that there's no way to use &mut self and you need to use interior mutability such as Cell<T> or RefCell<T>.

    I believe the most recent iteration of the component model disallows recursively entering a component, though, so a future iteration of wit-bindgen for the updated component model will likely define all methods with &mut self since you'll have exclusive access at all times.

  2. tqwewe commented on Mar 21, 2022

    @tqwewe
    Author

    Oh right! Thanks for all the info.
    For now I'll use Cell or RefCell, and with that update I guess I can easily modify my code to use &mut self instead.

    I truly appreciate the hard work you guys are doing. Feel free to close this issue if you think it's not needed, or it can be kept open as a tracker until &mut self.

  3. alexcrichton commented on Mar 21, 2022

    @alexcrichton
    Member

    Ok I'll leave this open so when we see this again I can confirm that this change was made or not.

  4. alexcrichton commented on Apr 20, 2022

    @alexcrichton
    Member

    Ok I'm going to close this in favor of #201 since this is part of the new canonical ABI updates.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions