jeromefroe/lru-rs

RwLock read() usage

Closed this issue · 1 comments

Hello, not sure if this is an issue, or it is intended to work like this, or it's just my fault (I'm quite new to Rust), but I can't make lru work within a RwLock.

Here's a sample test I wrote:

#[test]
fn readLockTest(){
    let mut cache: LruCache<String,String> = LruCache::new(100);
    cache.put("test".to_string(), "test_value".to_string());
    let lock_cache = RwLock::new(cache);
    let rc = lock_cache.read();
    let res = rc.unwrap().get("test");
    assert_eq!(res.unwrap().as_str(), "test_value");
}

This fails to compile with this message:

error[E0596]: cannot borrow data in a dereference of `RwLockReadGuard<'_, LruCache<String, String>>` as mutable
   --> tests/cache_test.rs:295:15
    |
295 |     let res = rc.unwrap().get("test");
    |               ^^^^^^^^^^^ cannot borrow as mutable
    |
    = help: trait `DerefMut` is required to modify through a dereference, but it is not implemented for `RwLockReadGuard<'_, LruCache<String, String>>`

Am I missing something?
Thank you

As I suspected, I was doing something wrong. No issue at all here. Sorry for the noise.