-
Notifications
You must be signed in to change notification settings - Fork 859
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add missing message handling (#200)
* feat: add missing message handling * refactor: new block message handling * feat: add events and commands for transaction handling * more work in transactions * chore: silence warnings
- Loading branch information
Showing
16 changed files
with
655 additions
and
129 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
use linked_hash_set::LinkedHashSet; | ||
use std::{borrow::Borrow, hash::Hash, num::NonZeroUsize}; | ||
|
||
/// A minimal LRU cache based on a `LinkedHashSet` with limited capacity. | ||
/// | ||
/// If the length exceeds the set capacity, the oldest element will be removed | ||
/// In the limit, for each element inserted the oldest existing element will be removed. | ||
#[derive(Debug, Clone)] | ||
pub struct LruCache<T: Hash + Eq> { | ||
limit: NonZeroUsize, | ||
inner: LinkedHashSet<T>, | ||
} | ||
|
||
impl<T: Hash + Eq> LruCache<T> { | ||
/// Creates a new `LruCache` using the given limit | ||
pub fn new(limit: NonZeroUsize) -> Self { | ||
Self { inner: LinkedHashSet::new(), limit } | ||
} | ||
|
||
/// Insert an element into the set. | ||
/// | ||
/// If the element is new (did not exist before [`LruCache::insert()`]) was called, then the | ||
/// given length will be enforced and the oldest element will be removed if the limit was | ||
/// exceeded. | ||
/// | ||
/// If the set did not have this value present, true is returned. | ||
/// If the set did have this value present, false is returned. | ||
pub fn insert(&mut self, entry: T) -> bool { | ||
if self.inner.insert(entry) { | ||
if self.limit.get() == self.inner.len() { | ||
// remove the oldest element in the set | ||
self.inner.pop_front(); | ||
} | ||
return true | ||
} | ||
false | ||
} | ||
|
||
/// Returns `true` if the set contains a value. | ||
pub fn contains<Q: ?Sized>(&self, value: &Q) -> bool | ||
where | ||
T: Borrow<Q>, | ||
Q: Hash + Eq, | ||
{ | ||
self.inner.contains(value) | ||
} | ||
} | ||
|
||
impl<T> Extend<T> for LruCache<T> | ||
where | ||
T: Eq + Hash, | ||
{ | ||
fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) { | ||
for item in iter.into_iter() { | ||
self.insert(item); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.