Guest User

Untitled

a guest
Dec 10th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.49 KB | None | 0 0
  1. #![feature(nll)]
  2.  
  3. use std::collections::HashMap;
  4.  
  5. #[derive(Debug, Copy, Clone)]
  6. struct Foo;
  7. struct FooSource;
  8. impl FooSource {
  9. fn get(&self, _id: &str) -> Foo { Foo }
  10. }
  11.  
  12. #[derive(Debug, Clone)]
  13. struct FooCache {
  14. foos: HashMap<String, Foo>,
  15. }
  16.  
  17. impl FooCache {
  18. fn new() -> FooCache {
  19. FooCache { foos: HashMap::new() }
  20. }
  21.  
  22. fn fetch<'a>(&'a mut self, foo_source: &FooSource, id: &str) -> &'a Foo {
  23. self.foos.entry(id.to_string()).or_insert_with(|| foo_source.get(id))
  24. }
  25. }
Add Comment
Please, Sign In to add comment