contain-rs / lru-cache

A cache that holds a limited number of key-value pairs
https://contain-rs.github.io/lru-cache/lru_cache/
Apache License 2.0
83 stars 36 forks source link

Is it possible to do a immutable borrow? #36

Open bbigras opened 7 years ago

bbigras commented 7 years ago

Something like:

extern crate lru_cache;       

use lru_cache::LruCache;
use std::sync::RwLock;

fn main() {                   
    let mut cache = LruCache::new(2);
    cache.insert(1, 10);

    let rwlock = RwLock::new(cache);
    let lock = rwlock.read().unwrap();
    let value = lock.get_mut(&1);    
    println!("{:?}", value);
}

I got:

error: cannot borrow immutable borrowed content as mutable
  --> src\main.rs:12:17
   |
12 |     let value = lock.get_mut(&1);
   |                 ^^^^
apasel422 commented 7 years ago

I assume you don't want to use RwLock::write, right? It would let you get a mutable reference to the LruCache. We could add a get-like method that takes &self and just doesn't update the LRU state.

bbigras commented 7 years ago

I assume you don't want to use RwLock::write, right?

Right. I would like to it with RwLock since we can have many read locks at the same time.

apasel422 commented 7 years ago

I'd accept a pull request that does this.