Skip to content

Modify the value of a field of a field that is from an Arc<Mutex<_>>/Rc<RefCell<_>> #546

Answered by khvzak
tkr-sh asked this question in Q&A

You must be logged in to vote

There are couple of options:

  1. Make VecWrapper acts as a vector, rather than a userdata that holds a vector.
impl UserData for VecWrapper {
    fn add_methods<M: UserDataMethods<Self>>(methods: &mut M) {
        methods.add_meta_method_mut("__index", |_, this, index: usize| Ok(this.vec.get(index).cloned()));
        methods.add_meta_method_mut("__newindex", |_, this, (index, value): (usize, u8)| {
            let _ = this.vec.get_mut(index).map(|slot| *slot = value);
            Ok(())
        });
    }
}
  1. If you don't want to wrap Vec<u8> to Arc<Mutex<Vec<u8>> then consider the following trick:
impl UserData for VecWrapper {
    fn add_fields<F: UserDataFields<Self>>(fields: &mut F) {

Replies: 1 comment 1 reply

You must be logged in to vote
1 reply
@tkr-sh

Answer selected by tkr-sh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants