Struct xorf::HashProxy

source ·
pub struct HashProxy<T, H, F>
where T: Hash, H: Hasher + Default, F: Filter<u64>,
{ /* private fields */ }
Expand description

Arbitrary key type proxy for xor filters.

A HashProxy exposes a Filter trait for arbitrary key types, using a Filter<u64> as an underlying keystore. The performance and collision rate of the HashProxy filter depends on the choice of Hasher and underlying Filter. A HashProxy is immutable once constructed.

use std::collections::hash_map::DefaultHasher;
use xorf::{Filter, HashProxy, Xor8};

const SAMPLE_SIZE: usize = 1_000_000;
let passwords: Vec<String> = (0..SAMPLE_SIZE)
    .map(|_| rand::thread_rng().sample_iter(&Alphanumeric).take(30).map(char::from).collect())
    .collect();

let pw_filter: HashProxy<String, DefaultHasher, Xor8> = HashProxy::from(&passwords);

for password in passwords {
    assert!(pw_filter.contains(&password));
}

While a HashProxy persists type information about the keys it is constructed with, in most cases the key type parameter can be elided. For example, the pw_filter defined above can also be defined as

let pw_filter: HashProxy<_, DefaultHasher, Xor8> = HashProxy::from(&passwords);

Because of HashProxys’ key type parameter, the existence of a key can only be checked using types a HashProxy is constructed with.

use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};
use xorf::{Filter, HashProxy, Xor8};

let fruits = vec!["apple", "banana", "tangerine", "watermelon"];
let fruits: HashProxy<_, DefaultHasher, Xor8> = HashProxy::from(&fruits);

let mut hasher = DefaultHasher::default();
"tangerine".hash(&mut hasher);
let tangerine_hash = hasher.finish();

assert!(fruits.contains(&tangerine_hash)); // doesn't work!

Serializing and deserializing HashProxys can be enabled with the serde feature.

Trait Implementations§

source§

impl<T, H, F> Filter<T> for HashProxy<T, H, F>
where T: Hash, H: Hasher + Default, F: Filter<u64>,

source§

fn contains(&self, key: &T) -> bool

Returns true if the underlying filter contains the specified key.

source§

fn len(&self) -> usize

Returns the number of fingerprints in the filter.
source§

impl<T, H, F> From<&[T]> for HashProxy<T, H, F>
where T: Hash, H: Hasher + Default, F: Filter<u64> + From<Vec<u64>>,

source§

fn from(keys: &[T]) -> Self

Converts to this type from the input type.
source§

impl<T, H, F> From<&Vec<T>> for HashProxy<T, H, F>
where T: Hash, H: Hasher + Default, F: Filter<u64> + From<Vec<u64>>,

source§

fn from(v: &Vec<T>) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<T, H, F> RefUnwindSafe for HashProxy<T, H, F>

§

impl<T, H, F> Send for HashProxy<T, H, F>
where F: Send, H: Send, T: Send,

§

impl<T, H, F> Sync for HashProxy<T, H, F>
where F: Sync, H: Sync, T: Sync,

§

impl<T, H, F> Unpin for HashProxy<T, H, F>
where F: Unpin, H: Unpin, T: Unpin,

§

impl<T, H, F> UnwindSafe for HashProxy<T, H, F>
where F: UnwindSafe, H: UnwindSafe, T: UnwindSafe,

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> 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<T, U> TryFrom<U> for T
where U: Into<T>,

§

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>,

§

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.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V