Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #![feature(nll)]
- use std::collections::HashMap;
- #[derive(Debug, Copy, Clone)]
- struct Foo;
- struct FooSource;
- impl FooSource {
- fn get(&self, _id: &str) -> Foo { Foo }
- }
- #[derive(Debug, Clone)]
- struct FooCache {
- foos: HashMap<String, Foo>,
- }
- impl FooCache {
- fn new() -> FooCache {
- FooCache { foos: HashMap::new() }
- }
- fn fetch<'a>(&'a mut self, foo_source: &FooSource, id: &str) -> &'a Foo {
- self.foos.entry(id.to_string()).or_insert_with(|| foo_source.get(id))
- }
- }
Add Comment
Please, Sign In to add comment