jaemk / cached

Rust cache structures and easy function memoization

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Behavior when function returns a `Result<T, E>`?

phayes opened this issue · comments

Reading the source code, it doesn't look like cached treats a Result<T, E> any differently than other values, and will cache it even if it's an Err. Is this understanding correct?

If so, are you able to provide an examples of how we might only cache values that are Ok<T> and not cache values that are Err<E>?

If my reading of the code is incorrect and this is the behavior already, perhaps it can be documented?

Hi @phayes , your understanding is correct in that it will cache both Oks and Errs by default. If you only want to cache Oks, then you can pass #[cached(result = true)]. It's mentioned in the documentation here (https://docs.rs/cached/latest/cached/proc_macro/attr.cached.html) where each macro argument is explained

Thanks @jaemk ,

OK if I open a PR to copy that list of properties to the main README. I looked but didn't find it, and I bet it's true for others as well.

Hm, so the readme is generated from the crate documentation in src/lib.rs (using cargo-readme, see the readme.sh script). There's actually links to each of the macros in the first paragraph, but they only show as links on the actual doc page https://docs.rs/cached/latest/cached/ and don't show up as links in the generated readme...

Do you think fixing these links, or calling them out more specifically (like "see documentation of each macro for optional arguments... Links here...") would be enough? I'd prefer that the documentation of the macro args stays in one place so it doesn't get out of sync