Implement a CachedValue Type

Hi all

I would like to implement a CachedValue Type.
See Code: https://gist.github.com/dns2utf8/d1db02cd530bd809371b

The intention was to hand over a Base, to new::<T, U>( :T, :U: Duration) so that every call over value() would check the lifetime and if exceeded call the updateFunction

Do you have any hints?

Regards

  1. You might want to implement Deref that might not be possible depending on your needs.
  2. Use something like a Cell to allow interior mutability.

Questions:

Do you need to access these values from multiple threads at once? If so, you'll need to use some form of lock (Mutex, RWLock).

What do you want to do in the following case?

let a = &*my_cached_value; // Deref to the inner value.
// time passes
let b = &*my_cached_value; // Tries to recompute the value.

In this case, the value is still borrowed by a so it can't be recomputed. Is that OK? If not, you can't implement Deref because Deref borrows.