Coder Social home page Coder Social logo

Comments (5)

rohitjoshi avatar rohitjoshi commented on September 26, 2024 1

@jeromefroe I have similar issue where Key is Vec<u8> but required to access using &[u8]. With the nightly flag, it works fine but I would like to use Rust stable release in production. This is the only functionality (to avoid memory allocation) which requires nightly feature. Any way to solve without using nightly release?

from lru-rs.

jeromefroe avatar jeromefroe commented on September 26, 2024

Hey @pimeys! Do you have a reproducible example by chance? I made a simple example and was able to call the get method on a LruCache<String, T> with a &str key, so I think something else may be going on. My example is:

extern crate lru;

use lru::LruCache;
use std::string::String;

fn main() {
  let mut cache: LruCache<String, i32> = LruCache::new(2);

  cache.put("apple".to_owned(), 1);

  let key: String = "apple".to_owned();
  assert_eq!(*cache.get(&key).unwrap(), 1);
}

from lru-rs.

pimeys avatar pimeys commented on September 26, 2024

What I want to do here is to skip the extra allocation when fetching from cache, and this doesn't work:

use lru::LruCache;

fn main() {
    let mut cache: LruCache<String, i32> = LruCache::new(2);
    
    cache.put("apple".to_owned(), 1);
    assert_eq!(cache.get("apple"), Some(1));
}

And in our case what we do is we check first is the item in the cache (first allocation for the fetch), then log if we hit the cache, or if not store to the Lru (second allocation for storage) and then again fetch from the cache.

I'd be much happier if we could use &str to fetch a key from a cache holding String as its key. They both are Eq and Hash, so it should be possible, as it is in std:collections::HashMap.

Or I change our interfaces to take &String instead of &str, but that would break all the interfaces only for caching, which also doesn't feel right.

from lru-rs.

jeromefroe avatar jeromefroe commented on September 26, 2024

Hey, sorry, you're right, this doesn't work by default. You'll have to enable the nightly feature to do this. The change to support this was originally added in #40 but I noticed that it didn't keep up to date with some of the changes in the nightly compiler so I addressed those issues in #86 and then released v0.5.2.. To get this to work, therefore, your Cargo.toml should contain the following:

[dependencies.lru]
version = "0.5.2"
features = ["nightly"]

Let me know if that fixes things.

from lru-rs.

pickfire avatar pickfire commented on September 26, 2024

version 0.6.6 and still seemed to have this issue

from lru-rs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.