Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible to do a immutable borrow? #36

Open
bbigras opened this issue Oct 7, 2016 · 3 comments
Open

Is it possible to do a immutable borrow? #36

bbigras opened this issue Oct 7, 2016 · 3 comments

Comments

@bbigras
Copy link

bbigras commented Oct 7, 2016

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
Copy link
Contributor

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
Copy link
Author

bbigras commented Oct 8, 2016

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
Copy link
Contributor

I'd accept a pull request that does this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants