Apologies if what I'm asking has a completely obvious solution, but I just can't see it.
The Rust book section Limitations of the Cacher Implementation ends by saying this:
Try modifying
Cacherto hold a hash map rather than a single value. The keys of the hash map will be theargvalues that are passed in, and the values of the hash map will be the result of calling the closure on that key. Instead of looking at whetherself.valuedirectly has aSomeor aNonevalue, thevaluefunction will look up theargin the hash map and return the value if it’s present. If it’s not present, theCacherwill call the closure and save the resulting value in the hash map associated with itsargvalue.The second problem with the current
Cacherimplementation is that it only accepts closures that take one parameter of typeu32and return au32. We might want to cache the results of closures that take a string slice and returnusizevalues, for example. To fix this issue, try introducing more generic parameters to increase the flexibility of theCacherfunctionality.
I gave it a go, and ended up with this code. But this will cause the argument of the closure function to be inferred as &{unknown} , is there any way to solve this other than manually marking the type?
