Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to update a mutable attribute? #140

Open
rirze opened this issue Jan 19, 2025 · 2 comments
Open

How to update a mutable attribute? #140

rirze opened this issue Jan 19, 2025 · 2 comments

Comments

@rirze
Copy link

rirze commented Jan 19, 2025

Hi, I'm struggling to find an implementation of a complex_value type that allows for mutation of its attributes. I'm confused on a number of aspects and I was hoping to clear them up with some help. Suppose I want a Value like this:

#[derive(Debug, ProvidesStaticType, NoSerialize, Allocative, Clone, Default, Display)]
#[display("myvalue {:?}", providers)]
struct MyValueGen {
    mapping: HashMap<String, AnotherStruct>,
}

impl MyValueGen {
    fn new() -> Self {
        MyValueGen {
            mapping: HashMap::new(),
        }
    }
}

starlark_complex_value!(MyValue);

How do I implement a method that can be called from starlark that can update the HashMap with a new pair of items. Any attempt I try tells me that I can't get a mutable reference to that attribute.

I've found #126, but the complicated example confuses me here. Do I create another struct that is Mutable w/ a RefCell and use that for all operations? That seems like a lot of boilerplate....

If I could get a hint, then I think it would really help me get started. Thanks!

@ndmitchell
Copy link
Contributor

You will need to use a RefCell for all operations.

Assuming you also want to use this for frozen values (e.g. non-mutable), you'll have to have both a frozen version and a non-frozen version.

And yeah, it is a fair bit of boilerplate. Unfortunately we couldn't figure out a way to abstract over mutability in Rust, so this was the best we could figure out.

@rirze
Copy link
Author

rirze commented Jan 19, 2025

I see... It would really help to have an illustrative example.

I'll try to get a working example and create a PR, but if you have a code sample in-mind/already that could be used, I'd love to learn from it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants