Interior Mutability with Traits


#1

Is it good to combine Cell and Trait so unique objects have their own persistent working data?

Something along the lines of:

struct O { Data: Cell<i32> }

with

<Vec<Box<T>>

impl T for O {
    fn Method(&self) {
        self.Data.get();
        self.Data.set();
    }
}

impl T for O2 {
    fn Method(&self) {
        self.Other.get();
        self.Other.set();
    }
}

What if I just want the entire object to be mutable from within a trait? (Should I?)

What about outside of the trait? (Specifically, since it’s wrapped in a Box.)


#2

Generally the traits methods should be &mut self. Interior mutability typically should only be used when linked to unobservable data, rather than something core to the structures functionality.