1
1
#![doc = include_str!("./crate-docs.md")]
2
#![forbid(unsafe_code)]
3
#![warn(
4
    clippy::cargo,
5
    missing_docs,
6
    // clippy::missing_docs_in_private_items,
7
    clippy::nursery,
8
    clippy::pedantic,
9
    future_incompatible,
10
    rust_2018_idioms,
11
)]
12
#![allow(
13
    clippy::missing_errors_doc, // TODO clippy::missing_errors_doc
14
    clippy::option_if_let_else,
15
    clippy::module_name_repetitions,
16
    clippy::cast_sign_loss,
17
    clippy::cast_possible_truncation
18
)]
19

            
20
mod hashed;
21
mod lru;
22
mod ordered;
23

            
24
use std::borrow::Borrow;
25
use std::hash::Hash;
26

            
27
pub use crate::hashed::*;
28
use crate::lru::{EntryCache, IntoIter};
29
pub use crate::lru::{EntryRef, Iter, Removed};
30
pub use crate::ordered::*;
31

            
32
/// A Least Recently Used map interface that supports all map implementations
33
/// exposed by this crate.
34
pub trait LruMap<Key, Value>:
35
    IntoIterator<Item = (Key, Value), IntoIter = IntoIter<Key, Value>> + EntryCache<Key, Value> + Sized
36
{
37
    /// Creates a new map with the maximum `capacity`.
38
    ///
39
    /// # Panics
40
    ///
41
    /// Panics if `capacity` is <= 1 or > `u32::MAX`.
42
    fn new(capacity: usize) -> Self;
43

            
44
    /// Returns the number of keys present in this map.
45
    fn len(&self) -> usize;
46

            
47
    /// Retruns true if this map contains no keys.
48
4
    fn is_empty(&self) -> bool {
49
4
        self.len() == 0
50
4
    }
51

            
52
    /// Returns a reference to the most recently used key.
53
    fn head(&mut self) -> Option<EntryRef<'_, Self, Key, Value>>;
54
    /// Returns a reference to the least recently used key.
55
    fn tail(&mut self) -> Option<EntryRef<'_, Self, Key, Value>>;
56

            
57
    /// Returns an iterator over the keys and values in order from most recently
58
    /// touched to least recently touched.
59
    fn iter(&self) -> Iter<'_, Key, Value>;
60

            
61
    /// Returns the stored value for `key`, if present.
62
    ///
63
    /// This function touches the key, making it the most recently used key.
64
    fn get<QueryKey>(&mut self, key: &QueryKey) -> Option<&Value>
65
    where
66
        QueryKey: Ord + Hash + Eq + ?Sized,
67
        Key: Borrow<QueryKey> + Ord + Hash + Eq;
68

            
69
    /// Returns the stored value for `key`, if present.
70
    ///
71
    /// This function does not touch the key, preserving its current position in
72
    /// the lru cache.
73
    fn get_without_update<QueryKey>(&self, key: &QueryKey) -> Option<&Value>
74
    where
75
        QueryKey: Ord + Hash + Eq + ?Sized,
76
        Key: Borrow<QueryKey> + Ord + Hash + Eq;
77

            
78
    /// Returns an [`EntryRef`] for `key`, if present.
79
    ///
80
    /// This function does not touch the key, preserving its current position in
81
    /// the lru cache. The [`EntryRef`] can touch the key, depending on which
82
    /// functions are used.
83
    fn entry<QueryKey>(&mut self, key: &QueryKey) -> Option<EntryRef<'_, Self, Key, Value>>
84
    where
85
        QueryKey: Ord + Hash + Eq + ?Sized,
86
        Key: Borrow<QueryKey> + Ord + Hash + Eq;
87

            
88
    /// Inserts `value` for `key` into this map. If a value is already stored
89
    /// for this key, [`Removed::PreviousValue`] is returned with the previously
90
    /// stored value. If no value is currently stored and the map is full, the
91
    /// least recently used entry will be returned in [`Removed::Evicted`].
92
    /// Otherwise, `None` will be returned.
93
    ///
94
    /// This function touches the key, making it the most recently used key.
95
    fn push(&mut self, key: Key, value: Value) -> Option<Removed<Key, Value>>;
96

            
97
    /// Pushes all items from `iterator` into this map. If there are more
98
    /// entries in the iterator than capacity remaining, keys will be evicted as
99
    /// needed.
100
    ///
101
    /// This function is equivalent to a for loop calling [`Self::push()`].
102
    fn extend<IntoIter: IntoIterator<Item = (Key, Value)>>(&mut self, iterator: IntoIter);
103
}
104

            
105
#[cfg(test)]
106
mod tests;