sile / scalable_cuckoo_filter

A variant of Cuckoo Filter whose size automatically scales as necessary
MIT License
17 stars 7 forks source link

Question about using ScalableCuckooFilterBuilder #14

Closed ashtul closed 5 months ago

ashtul commented 5 months ago

Hi @sile, I am trying to implement the crate into a project. Using the following code -

let mut filter ScalableCuckooFilter = ScalableCuckooFilterBuilder::new()  
                .initial_capacity(params.capacity as usize)  
                .false_positive_probability(params.err)
                .entries_per_bucket(params.bucket_size as usize)    
                .max_kicks(params.max_iterations as usize)
                .finish();

an error is reported by the compiler -


error[E0283]: type annotations needed for `ScalableCuckooFilter<T>`
   --> src/lib.rs:111:9
    |
111 |     let mut filter = ScalableCuckooFilterBuilder::new()  
    |         ^^^^^^^^^^
...
117 |                 .finish();
    |                  ------ type must be known at this point
    |
    = note: cannot satisfy `_: std::hash::Hash`
note: required by a bound in `scalable_cuckoo_filter::ScalableCuckooFilterBuilder::<H, R>::finish`
   --> /Users/arielshtul/.cargo/registry/src/index.crates.io-6f17d22bba15001f/scalable_cuckoo_filter-0.1.2/src/scalable_cuckoo_filter.rs:107:22
    |
107 |     pub fn finish<T: Hash + ?Sized>(self) -> ScalableCuckooFilter<T, H, R> {
    |                      ^^^^ required by this bound in `ScalableCuckooFilterBuilder::<H, R>::finish`
help: consider giving `filter` an explicit type, where the type for type parameter `T` is specified
    |
111 |     let mut filter: ScalableCuckooFilter<T> = ScalableCuckooFilterBuilder::new()  

How can this be fixed?

ashtul commented 5 months ago

I was able to fix it with

let filter: ScalableCuckooFilter<&str> = ScalableCuckooFilterBuilder::new()

but wondered if there is another way?

sile commented 5 months ago

You need to provide type annotations for the ScalableCuckooFilter as specified by the compiler message. The following code is an example:

let mut filter: ScalableCuckooFilter<&str, _, _> = ScalableCuckooFilterBuilder::new()
        .initial_capacity(params.capacity as usize)
        .false_positive_probability(params.err)
        .entries_per_bucket(params.bucket_size as usize)
        .max_kicks(params.max_iterations as usize)
        .finish();