0

I have the following scenario:

struct Foo {
    complex_data: RwLock<ComplexData>,
    instant: Instant,
}

impl Foo {
    update(&self) {
        let mut complex_data = self.complex_data.write();
        complex_data.mut_update();
        // I can't re-assign Instant since I'm only taking `&self` not `&mut self` 
    }
}

I want to be able to re-assign the value of instant inside the update method without changing update to take &mut self. What's the best way to do this? (Is it to use a Box?)

Foobar
  • 7,458
  • 16
  • 81
  • 161
  • 1
    You don't. That's literally the point of the `mut` mechanism. You're not supposed to. – Silvio Mayolo Mar 28 '21 at 23:44
  • why not just ... take &mut self ? – Stargateur Mar 29 '21 at 00:01
  • 1
    Does this answer your question? [Situations where Cell or RefCell is the best choice](https://stackoverflow.com/questions/30831037/situations-where-cell-or-refcell-is-the-best-choice) – Stargateur Mar 29 '21 at 00:03
  • @SilvioMayolo Have you heard of concurrent data structures? Sometimes... you want to call a method on a struct in different threads! – Foobar Mar 29 '21 at 00:15
  • @Stargateur I read it over, but I haven't been able to implement a non-`Box` solution from the materials present. – Foobar Mar 29 '21 at 01:06
  • @Roymunson I didn't use Cell much so I can't tell, anyway the point is that if you don't need share this feature between thread you should use Cell not Mutex. The point of Cell is having interior mutability. Your question is not very clear so It's hard to answer you perfectly. Your use case of update a time is often a good use case for interior mutability so I proposed to use Cell. – Stargateur Mar 29 '21 at 01:12

1 Answers1

0

I ended up using a Mutex<Box<Instant>>, but I'm not sure if there's a better way. Forcing something to be on the heap seems unnecessary/slow?

struct Foo {
    complex_data: RwLock<ComplexData>,
    instant: Mutex<Instant>,
}

impl Foo {
    update(&self) {
        let mut complex_data = self.complex_data.write();
        complex_data.mut_update();
        // I can't re-assign Instant since I'm only taking `&self` not `&mut self` 
        let mut instant = self.instant.lock();
        *instant = Box::new(Instant::now());
    }
}
Foobar
  • 7,458
  • 16
  • 81
  • 161