Comments (9)
Gotcha, definitely seems useful! I'll take a crack at it when I get some cycles, but it will probably be a larger change so I don't anticipate I'll be able to get to it in the near future.
from lru-rs.
Hey @Ralith! I don't think I have a clear grasp of what your use case is. Would you mind providing an example?
From what I do understand, it sounds like you could use an additional method like the put
method but which only inserts the value if the key doesn't already exist in the cache and that indicates whether the new value was inserted or not by returning a reference to the value that is stored in the cache? Something like the following perhaps:
use lru::LruCache;
let mut cache = LruCache::new(2);
let first = cache.put_if_missing(1, "a");
assert_eq!(first, &"a");
let second = cache.put_if_missing(1, "b");
assert_eq!(second, &"a");
I don't like the name put_if_missing
but couldn't think of anything better right now :)
from lru-rs.
The use case, and desired semantics, are exactly that of the std HashMap::entry
API (see also BTreeMap
, etc). put_if_missing
isn't a solution because my use of LruCache
is to avoid repeating expensive computations, which requires waiting until the entry is known to be missing before computing its value.
from lru-rs.
Actually, this is even more needed cause I can't get a value, return a reference to it or if missing create the value and return it cause it's borrow the cache as mutable more than one. It's quite annoying for a cache to not be able to do this basic operation.
from lru-rs.
I think one question for this API will be at what point an occupied entry is considered "used" and moved to the back of the queue? I think there are broadly three options:
- When
OccupiedEntry
is created (i.e. whenLruCache::entry(&mut self, key: K)
is called for an extant key); - When the extant value is obtained from an
OccupiedEntry
(e.g. whenOccupiedEntry::get(&self)
etc are invoked); or - When
OccupiedEntry
is dropped. This has the advantage that no extraneous work is performed if the entry was removed from the cache.
from lru-rs.
Would love to see this implemented!
The stdlib has a method .entry(&key).or_insert(val)
which is extremely nice to have. Otherwise I can't think of a better way to write the following:
let value = match cache.get(&key) {
Some(value) => value,
None => {
let val = foo();
cache.put(key, val);
cache.get(key).expect("item should exist")
}
};
It would be much better to do:
let value = cache.entry(key).or_insert_with(foo);
from lru-rs.
Note that LruCache
recently got a get_or_insert
method; however that method always require a fully-owned key. This means that when using, for example, String
as keys, it's not possible to only allocate when a new entry needs to be inserted.
There's also no get_or_insert_mut
that would return a mutable reference to the (possibly newly-inserted) value.
Finally, the entry API has other advantages, like working better when the function to create the missing value can fail.
from lru-rs.
I also miss get_or_insert_mut
. When trying to use this as a multimap I first have to do a get_or_insert
followed by a get_mut
, first to populate a vec for a key, and then to actually place a new item in the vec.
from lru-rs.
Another use-case for the entry API is referencing the key in the insert
fn without reallocating it:
let k: String = "some-key".into();
let v = match map.entry(k) {
Entry::Occupied(e) => e.into_mut(),
Entry::Vacant(v) => {
v.insert(query_my_server_for_whatever(v.key().as_str())?)
}
};
from lru-rs.
Related Issues (20)
- RwLock read() usage HOT 1
- Consider replacing HashMap with HashSet HOT 1
- Use after free bug in lru crate HOT 2
- Am I able to cache egui? HOT 1
- Lifetime fix is missing from the changelog for 0.7.4 HOT 2
- Full check leak report reachable bytes HOT 1
- Method that removes an entry corresponding to the key and returns both the key and the value
- Failed to build with feature "nightly" HOT 1
- Allow more liberal closure for `get_or_insert` HOT 2
- Could we have a new version with the updated `hashbrown` (and maybe others) dependency ? HOT 2
- Support const new
- Limit by memory instead of number of items HOT 4
- Cache size 0 / Disable Cache HOT 3
- Cache key may not work HOT 5
- Debug not defined for custom BuildHasher
- Missing lifetime specifier in try_get_or_insert_mut? HOT 1
- Add `get_key_value` method
- Proposal to Integrate SIEVE Eviction Algorithm HOT 12
- Implement Clone on LruCache HOT 5
- Clone is only implemented for the default hasher HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from lru-rs.