diff --git a/src/pxar/fuse.rs b/src/pxar/fuse.rs index cfd3d929..d8b53e0a 100644 --- a/src/pxar/fuse.rs +++ b/src/pxar/fuse.rs @@ -81,8 +81,8 @@ struct Context { /// DirectoryEntry via the Decoder as well as the parent, in order /// to be able to include the parent directory on readdirplus calls. start_end_parent: HashMap, - gbt_cache: LruCache>, - entry_cache: LruCache, + gbt_cache: LruCache>, + entry_cache: LruCache, } /// Cacher for the goodbye table. @@ -93,7 +93,7 @@ struct GbtCacher<'a> { map: &'a HashMap, } -impl<'a> Cacher> for GbtCacher<'a> { +impl<'a> Cacher> for GbtCacher<'a> { fn fetch(&mut self, key: u64) -> Result>, Error> { let (end, _) = *self.map.get(&key).unwrap(); let gbt = self.decoder.goodbye_table(None, end)?; @@ -109,7 +109,7 @@ struct EntryCacher<'a> { map: &'a HashMap, } -impl<'a> Cacher for EntryCacher<'a> { +impl<'a> Cacher for EntryCacher<'a> { fn fetch(&mut self, key: u64) -> Result, Error> { let entry = match key { 0 => self.decoder.root()?, @@ -128,8 +128,8 @@ impl Context { fn as_mut_refs(&mut self) -> ( &mut Decoder, &mut HashMap, - &mut LruCache>, - &mut LruCache + &mut LruCache>, + &mut LruCache ) { ( &mut self.decoder, &mut self.start_end_parent, &mut self.gbt_cache, &mut self.entry_cache ) } @@ -347,8 +347,8 @@ impl Session { F: FnOnce( &mut Decoder, &mut HashMap, - &mut LruCache>, - &mut LruCache, + &mut LruCache>, + &mut LruCache, u64, ) -> Result<(), i32>, { diff --git a/src/tools/lru_cache.rs b/src/tools/lru_cache.rs index 745c5ab8..1e5963db 100644 --- a/src/tools/lru_cache.rs +++ b/src/tools/lru_cache.rs @@ -8,29 +8,29 @@ use std::collections::HashMap; use std::marker::PhantomData; /// Interface for getting values on cache misses. -pub trait Cacher { +pub trait Cacher { /// Fetch a value for key on cache miss. /// /// Whenever a cache miss occurs, the fetch method provides a corresponding value. /// If no value can be obtained for the given key, None is returned, the cache is /// not updated in that case. - fn fetch(&mut self, key: u64) -> Result, failure::Error>; + fn fetch(&mut self, key: K) -> Result, failure::Error>; } /// Node of the doubly linked list storing key and value -struct CacheNode { +struct CacheNode { // We need to additionally store the key to be able to remove it // from the HashMap when removing the tail. - key: u64, + key: K, value: V, - prev: *mut CacheNode, - next: *mut CacheNode, + prev: *mut CacheNode, + next: *mut CacheNode, // Dropcheck marker. See the phantom-data section in the rustonomicon. - _marker: PhantomData>>, + _marker: PhantomData>>, } -impl CacheNode { - fn new(key: u64, value: V) -> Self { +impl CacheNode { + fn new(key: K, value: V) -> Self { Self { key, value, @@ -49,7 +49,7 @@ impl CacheNode { /// # fn main() -> Result<(), failure::Error> { /// struct LruCacher {}; /// -/// impl Cacher for LruCacher { +/// impl Cacher for LruCacher { /// fn fetch(&mut self, key: u64) -> Result, failure::Error> { /// Ok(Some(key)) /// } @@ -88,16 +88,16 @@ impl CacheNode { /// # Ok(()) /// # } /// ``` -pub struct LruCache { - map: HashMap>, - head: *mut CacheNode, - tail: *mut CacheNode, +pub struct LruCache { + map: HashMap>, + head: *mut CacheNode, + tail: *mut CacheNode, capacity: usize, // Dropcheck marker. See the phantom-data section in the rustonomicon. - _marker: PhantomData>>, + _marker: PhantomData>>, } -impl LruCache { +impl LruCache { /// Create LRU cache instance which holds up to `capacity` nodes at once. pub fn new(capacity: usize) -> Self { Self { @@ -123,7 +123,7 @@ impl LruCache { /// Insert or update an entry identified by `key` with the given `value`. /// This entry is placed as the most recently used node at the head. - pub fn insert(&mut self, key: u64, value: V) { + pub fn insert(&mut self, key: K, value: V) { match self.get_mut(key) { // Key already exists and get_mut brings node to the front, so only update its value. Some(old_val) => *old_val = value, @@ -140,7 +140,7 @@ impl LruCache { /// Insert a key, value pair at the front of the linked list and it's pointer /// into the HashMap. - fn insert_front(&mut self, key: u64, value: V) { + fn insert_front(&mut self, key: K, value: V) { // First create heap allocated `CacheNode` containing value. let mut node = Box::new(CacheNode::new(key, value)); // Old head gets new heads next @@ -165,7 +165,7 @@ impl LruCache { } /// Remove the given `key` and its `value` from the cache. - pub fn remove(&mut self, key: u64) -> Option { + pub fn remove(&mut self, key: K) -> Option { // Remove node pointer from the HashMap and get ownership of the node let node_ptr = self.map.remove(&key)?; let node = unsafe { Box::from_raw(node_ptr) }; @@ -209,7 +209,7 @@ impl LruCache { /// Get a mutable reference to the value identified by `key`. /// This will update the cache entry to be the most recently used entry. /// On cache misses, None is returned. - pub fn get_mut<'a>(&'a mut self, key: u64) -> Option<&'a mut V> { + pub fn get_mut<'a>(&'a mut self, key: K) -> Option<&'a mut V> { let node_ptr = self.map.get(&key)?; if *node_ptr == self.head { // node is already head, just return @@ -251,7 +251,7 @@ impl LruCache { /// value. /// If fetch returns a value, it is inserted as the most recently used entry /// in the cache. - pub fn access<'a>(&'a mut self, key: u64, cacher: &mut dyn Cacher) -> Result, failure::Error> { + pub fn access<'a>(&'a mut self, key: K, cacher: &mut dyn Cacher) -> Result, failure::Error> { if self.get_mut(key).is_some() { // get_mut brings the node to the front if present, so just return return Ok(Some(unsafe { &mut (*self.head).value }));