jaemk / cached

Rust cache structures and easy function memoization
MIT License
1.58k stars 96 forks source link

How to aproach generics ? #80

Open big-lip-bob opened 3 years ago

big-lip-bob commented 3 years ago

Since apparently i can't really use cached with generic functions (let's say fn add_one<T: One + Add<Output=T>>(n: T) -> T { n + T::one() }), how would i proceed to make a cache for a specific type that's valid as said generic (let's use usize) ?

big-lip-bob commented 3 years ago

i am aware that i could specify the type, (as #[cached] fn add_one_usize(n: usize) -> usize { n + usize::one() }) (attempting at a macro now)

tdudz commented 3 years ago

@big-lip-bob did you ever resolve this?

big-lip-bob commented 3 years ago

No, i left it dormant as i didn't have much time since then

WalterSmuts commented 2 years ago

I ran into a similar issue and wrote the generic_singleton crate. It doesn't have any of the fancy macros cached has, mostly because it wasn't really aimed at writing a caching library, but there is no reason it cannot be done. Or perhaps even better, cached can use the generic_singleton crate to add the generic function caching feature :)