jaemk / cached

Rust cache structures and easy function memoization

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Cannot use key as a function argument

paulvt opened this issue · comments

I was really fighting confusing compile errors like:

error[E0382]: use of moved value: `key`
   --> src/backend.rs:162:1
    |
162 | #[cached(time = 3600, result = true)]
    | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    | |
    | value used here after move
    | move occurs because `key` has type `std::string::String`, which does not implement the `Copy` trait
163 | async fn test(key: String) -> Result<String> {
    |               --- value moved here
    |
    = note: this error originates in the attribute macro `cached` (in Nightly builds, run with -Z macro-backtrace for more info)

Until I released that the key for the store is probably calculated and bound to a variable key somewhere in the macro.
I think the library should use something like __caching_key to reduce the change on variable name clashes (maybe there are more?)
Possibly there is another really hygienic way to do this in macros that I am unaware of.