cached icon indicating copy to clipboard operation
cached copied to clipboard

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

Open phayes opened this issue 2 years ago • 3 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?

phayes avatar May 03 '22 17:05 phayes

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

jaemk avatar May 03 '22 19:05 jaemk

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.

phayes avatar May 03 '22 19:05 phayes

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

jaemk avatar May 04 '22 03:05 jaemk