Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct Cacher<T, U, V>
- where T: Fn(V) -> U
- {
- calculation: T,
- value: Option<U>,
- arg: V,
- }
- impl<T, U: Copy, V> Cacher<T, U, V>
- where T: Fn(V) -> U
- {
- fn new(calculation: T, arg: V) -> Cacher<T, U, V> {
- Cacher {
- calculation,
- value: None,
- arg
- }
- }
- fn value(&mut self, arg: V) -> U {
- match self.value {
- Some(v) => v,
- None => {
- let v = (self.calculation)(arg);
- self.value = Some(v);
- v
- },
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement