error[E0277]: the trait bound `&mut T: Clone` is not satisfied
--> src/traits.rs:281:5
|
281 | indicator: &'ema mut T,
| ^^^^^^^^^^^^^^^^^^^^^^ the trait `Clone` is not implemented for `&mut T`
|
= help: the following implementations were found:
<&T as Clone>
<&mut T as Clone>
= note: `Clone` is implemented for `&T`, but not for `&mut T`
= note: required by `clone`
= note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)
This can't possibly work. Cloning a mutable reference would create two mutable references to the same location. This is not allowed. There must either be a single mutable reference or any number of immutable references.
I follow what you are saying, and the reason I want to Clone it is because I have PriceIndicator which is then used to create multiple EMAIndicator inside MACDIndicator. So basically, how to address this problem for the new of MACDIndicator i.e. how do I use the Indicator field and the other field such as short_term_period and long-term_period. Also Mutable Reference because they contain vector which holds the cached results to avoid re-calcs.
Was trying to add RefCell. Is that the only possibility? Also from Design perspective it should be simpler than this and I may be doing something incorrect.
That is about the only possibility. There are also types like Cell and once_cell::unsync::OnceCell that are more specialized, but also more limited. You will have to use interior mutability in some way to mutate through an immutable reference.
oh ok, thanks. But this problem might crop in several places. If you happen to think of a better solution to this please let me know. Again thanks a lot for your time and help.
yes kind of makes sense and I think I need to rethink my design a bit more. Should not have had to do this as I never change the values for the PriceIndicators. It's the wrappers with the calculated values that need to be mutable. Also, I am yet not sure, inside the new method, is there a possibility to refer to another field? Still need to read the Rust Book