Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.56 KB | None | 0 0
  1. struct Cacher<T, U, V>
  2. where T: Fn(V) -> U
  3. {
  4. calculation: T,
  5. value: Option<U>,
  6. arg: V,
  7. }
  8.  
  9. impl<T, U: Copy, V> Cacher<T, U, V>
  10. where T: Fn(V) -> U
  11. {
  12. fn new(calculation: T, arg: V) -> Cacher<T, U, V> {
  13. Cacher {
  14. calculation,
  15. value: None,
  16. arg
  17. }
  18. }
  19.  
  20. fn value(&mut self, arg: V) -> U {
  21. match self.value {
  22. Some(v) => v,
  23. None => {
  24. let v = (self.calculation)(arg);
  25. self.value = Some(v);
  26. v
  27. },
  28. }
  29. }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement