Struct RegexCache

Source
pub struct RegexCache(/* private fields */);
Expand description

An LRU cache for regular expressions.

Implementations§

Source§

impl RegexCache

Source

pub fn new(capacity: usize) -> RegexCache

Create a new LRU cache with the given size limit.

Source

pub fn save(&mut self, re: Regex) -> &Regex

Save the given regular expression in the cache.

§Example
let mut cache = RegexCache::new(100);
let     re    = Regex::new(r"^\d+$").unwrap();

// By saving the previously created regular expression further calls to
// `compile` won't actually compile the regular expression.
cache.save(re);

assert!(cache.compile(r"^\d+$").unwrap().is_match("1234"));
assert!(!cache.compile(r"^\d+$").unwrap().is_match("abcd"));
Source

pub fn compile(&mut self, source: &str) -> Result<&Regex, Error>

Create a new regular expression in the cache.

§Example
let mut cache = RegexCache::new(100);

assert!(cache.compile(r"^\d+$").unwrap().is_match("1234"));
assert!(!cache.compile(r"^\d+$").unwrap().is_match("abcd"));
Source

pub fn configure<F>(&mut self, source: &str, f: F) -> Result<&Regex, Error>
where F: FnOnce(&mut RegexBuilder) -> &mut RegexBuilder,

Configure a new regular expression.

§Example
let mut cache = RegexCache::new(100);

assert!(cache.configure(r"abc", |b| b.case_insensitive(true)).unwrap()
	.is_match("ABC"));

assert!(!cache.configure(r"abc", |b| b.case_insensitive(true)).unwrap()
	.is_match("123"));

Methods from Deref<Target = LruCache<String, Regex>>§

Source

pub fn contains_key<Q>(&mut self, key: &Q) -> bool
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Checks if the map contains the given key.

§Examples
use lru_cache::LruCache;

let mut cache = LruCache::new(1);

cache.insert(1, "a");
assert_eq!(cache.contains_key(&1), true);
Source

pub fn insert(&mut self, k: K, v: V) -> Option<V>

Inserts a key-value pair into the cache. If the key already existed, the old value is returned.

§Examples
use lru_cache::LruCache;

let mut cache = LruCache::new(2);

cache.insert(1, "a");
cache.insert(2, "b");
assert_eq!(cache.get_mut(&1), Some(&mut "a"));
assert_eq!(cache.get_mut(&2), Some(&mut "b"));
Source

pub fn get_mut<Q>(&mut self, k: &Q) -> Option<&mut V>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Returns a mutable reference to the value corresponding to the given key in the cache, if any.

§Examples
use lru_cache::LruCache;

let mut cache = LruCache::new(2);

cache.insert(1, "a");
cache.insert(2, "b");
cache.insert(2, "c");
cache.insert(3, "d");

assert_eq!(cache.get_mut(&1), None);
assert_eq!(cache.get_mut(&2), Some(&mut "c"));
Source

pub fn remove<Q>(&mut self, k: &Q) -> Option<V>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Removes the given key from the cache and returns its corresponding value.

§Examples
use lru_cache::LruCache;

let mut cache = LruCache::new(2);

cache.insert(2, "a");

assert_eq!(cache.remove(&1), None);
assert_eq!(cache.remove(&2), Some("a"));
assert_eq!(cache.remove(&2), None);
assert_eq!(cache.len(), 0);
Source

pub fn capacity(&self) -> usize

Returns the maximum number of key-value pairs the cache can hold.

§Examples
use lru_cache::LruCache;
let mut cache: LruCache<i32, &str> = LruCache::new(2);
assert_eq!(cache.capacity(), 2);
Source

pub fn set_capacity(&mut self, capacity: usize)

Sets the number of key-value pairs the cache can hold. Removes least-recently-used key-value pairs if necessary.

§Examples
use lru_cache::LruCache;

let mut cache = LruCache::new(2);

cache.insert(1, "a");
cache.insert(2, "b");
cache.insert(3, "c");

assert_eq!(cache.get_mut(&1), None);
assert_eq!(cache.get_mut(&2), Some(&mut "b"));
assert_eq!(cache.get_mut(&3), Some(&mut "c"));

cache.set_capacity(3);
cache.insert(1, "a");
cache.insert(2, "b");

assert_eq!(cache.get_mut(&1), Some(&mut "a"));
assert_eq!(cache.get_mut(&2), Some(&mut "b"));
assert_eq!(cache.get_mut(&3), Some(&mut "c"));

cache.set_capacity(1);

assert_eq!(cache.get_mut(&1), None);
assert_eq!(cache.get_mut(&2), None);
assert_eq!(cache.get_mut(&3), Some(&mut "c"));
Source

pub fn remove_lru(&mut self) -> Option<(K, V)>

Removes and returns the least recently used key-value pair as a tuple.

§Examples
use lru_cache::LruCache;

let mut cache = LruCache::new(2);

cache.insert(1, "a");
cache.insert(2, "b");

assert_eq!(cache.remove_lru(), Some((1, "a")));
assert_eq!(cache.len(), 1);
Source

pub fn len(&self) -> usize

Returns the number of key-value pairs in the cache.

Source

pub fn is_empty(&self) -> bool

Returns true if the cache contains no key-value pairs.

Source

pub fn clear(&mut self)

Removes all key-value pairs from the cache.

Source

pub fn iter(&self) -> Iter<'_, K, V>

Returns an iterator over the cache’s key-value pairs in least- to most-recently-used order.

Accessing the cache through the iterator does not affect the cache’s LRU state.

§Examples
use lru_cache::LruCache;

let mut cache = LruCache::new(2);

cache.insert(1, 10);
cache.insert(2, 20);
cache.insert(3, 30);

let kvs: Vec<_> = cache.iter().collect();
assert_eq!(kvs, [(&2, &20), (&3, &30)]);
Source

pub fn iter_mut(&mut self) -> IterMut<'_, K, V>

Returns an iterator over the cache’s key-value pairs in least- to most-recently-used order, with mutable references to the values.

Accessing the cache through the iterator does not affect the cache’s LRU state.

§Examples
use lru_cache::LruCache;

let mut cache = LruCache::new(2);

cache.insert(1, 10);
cache.insert(2, 20);
cache.insert(3, 30);

let mut n = 2;

for (k, v) in cache.iter_mut() {
    assert_eq!(*k, n);
    assert_eq!(*v, n * 10);
    *v *= 10;
    n += 1;
}

assert_eq!(n, 4);
assert_eq!(cache.get_mut(&2), Some(&mut 200));
assert_eq!(cache.get_mut(&3), Some(&mut 300));

Trait Implementations§

Source§

impl Clone for RegexCache

Source§

fn clone(&self) -> RegexCache

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for RegexCache

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Deref for RegexCache

Source§

type Target = LruCache<String, Regex>

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl DerefMut for RegexCache

Source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.