al8n / stretto

Stretto is a Rust implementation for Dgraph's ristretto (https://github.com/dgraph-io/ristretto). A high performance memory-bound Rust cache.
Apache License 2.0
413 stars 28 forks source link

How to construct a TransparentKeyBuilder? #56

Closed sourcefrog closed 9 months ago

sourcefrog commented 11 months ago

This looks nice!

I'm trying this code:

        let cache = stretto::Cache::new_with_key_builder(
            max_counters,
            MAX_CACHE_BYTES as i64,
            stretto::TransparentKeyBuilder::default(),
        )
        .expect("Create blockdir cache");

However, it complains that my key type doesn't implement Default, which it doesn't, and I don't really want to since there is no sensible default

error[E0277]: the trait bound `blockhash::BlockHash: std::default::Default` is not satisfied
   --> src/blockdir.rs:109:13
    |
109 |             stretto::TransparentKeyBuilder::default(),
    |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::default::Default` is not implemented for `blockhash::BlockHash`
    |
    = help: the trait `std::default::Default` is implemented for `stretto::TransparentKeyBuilder<K>`
    = note: required for `stretto::TransparentKeyBuilder<blockhash::BlockHash>` to implement `std::default::Default`

It doesn't seem inherently necessary that the key has a default? Could we just add a new method?

al8n commented 10 months ago

Hi, TransparentKeyBuilder only supports types which implement TransparentKey trait, if you want to use it, you must implement TransparentKey for your key type first.

sourcefrog commented 10 months ago

I think the problem I hit was connected to this:

https://github.com/al8n/stretto/blob/cccb12b14fc612a5e8f2ac595e04c995036d4212/src/lib.rs#L457-L461

I tried implementing TransparentKey, but the problem is that the derive(Default) implies that my key type K must also implement Default. I feel like for many keys, at least for my key, there is no very meaningful defaulti

sourcefrog commented 9 months ago

I tried to make a small repro, but it seems to work OK when the key type is !Default. Perhaps I was just confused.