mqfilters/lib.rs
1pub mod error;
2pub use error::{QueryFilterError, QueryFilterResult};
3
4#[cfg(feature = "bf")]
5pub mod bf;
6
7use std::{borrow::Borrow, hash::Hash};
8
9#[cfg(feature = "bf")]
10pub use bf::BloomFilter;
11
12/// Defines membership query filter.
13///
14/// Each implementation of `QueryFilter` defines a filter that can be used to
15/// query whether an element is a member of a set or not. False positives are
16/// allowed (although considerable effort should be made to minimize them).
17/// False negatives are not allowed.
18pub trait QueryFilter<K> {
19 /// Returns `true` if the value is believed to be in the filter.
20 ///
21 /// The value may be any borrowed form of the filter's key type, but
22 /// [`Hash`] and [`Eq`] on the borrowed form *must* match those for
23 /// the inserted key type.
24 fn contains<Q>(&self, key: &Q) -> bool
25 where
26 K: Borrow<Q>,
27 Q: Eq + Hash + ?Sized;
28}
29
30/// Defines a filter that supports adding elements.
31pub trait InsertableQueryFilter<K>: QueryFilter<K> {
32 /// Inserts an element into the filter.
33 fn insert(&mut self, key: K)
34 where
35 K: Eq + Hash;
36}
37
38/// Defines a filter that supports removal of elements.
39pub trait RemovableQueryFilter<K>: QueryFilter<K> {
40 /// Removes an element from the filter.
41 fn remove<Q>(&mut self, key: &Q)
42 where
43 K: Borrow<Q>,
44 Q: Eq + Hash + ?Sized;
45}
46
47/// Defines a filter that supports clearing all elements.
48pub trait ClearableQueryFilter<K>: QueryFilter<K> {
49 /// Removes all elements from the filter.
50 fn clear(&mut self);
51}