memoize icon indicating copy to clipboard operation
memoize copied to clipboard

`memoize` macro requires owned parameters

Open haydnv opened this issue 1 year ago • 2 comments

This is similar to #20 but narrower in scope. The HashMap or LruCache that stores memoized results only requires an owned key when a new value is inserted. Could the memoized function accept a borrowed key which implements Clone or maybe ToOwned? Then if the function signature is fn my_function(key: &String) -> Value the cache could call key.clone() or key.to_owned() only in the case of a cache insert.

haydnv avatar Nov 16 '23 11:11 haydnv

That's a great idea, I will take a look at it

dermesser avatar Dec 01 '23 16:12 dermesser

Also came here to ask would you consider generalizing Clone to ToOwned?

It would probably be a simple change if not for the fact that HashMap is used in the signatures of the __init and __getit functions.

I don't know a generic way to get the to_owned() return type, but if not the signatures, type inference should handle this just fine.

sanchopanca avatar Dec 15 '23 16:12 sanchopanca