Coder Social home page Coder Social logo

Cache size 0 / Disable Cache about lru-rs HOT 3 OPEN

PSeitz avatar PSeitz commented on September 27, 2024 2
Cache size 0 / Disable Cache

from lru-rs.

Comments (3)

behzadnouri avatar behzadnouri commented on September 27, 2024

+1 to this.

@jeromefroe Can you please consider reverting #150 which uses NonZeroUsize for capacity.

There are certain cases which capacity zero is pretty useful and ergonomic in the code as in for example to disable the cache mentioned above. It is a similar thing to pass an empty vector or buffer around.
Or you may be dynamically allocating capacity to each of many caches and some might get zero.

NonZeroUsize is also making the api more divergent from standard hash-map type (or other common types). Having to use NonZeroUsize also forces to either use the unsafe new_unchecked function or use unwrap after new, and both are ugly.

from lru-rs.

Yosi-Hezi avatar Yosi-Hezi commented on September 27, 2024

If you wish to support a cache with zero capacity, you will need to return an Option whenever you call get_or_insert or get_mut_or_insert. This means handling the case where the capacity is zero each time instead of just during initialization. Is this justified? I think that in most use cases the desired change will add much more branching (handling Options).

Comparing it to a hash-map type may not be suitable because hash-maps typically have dynamic capacity and don't fail on insertions due to capacity limitations, as it increases on demand.

Still, it should be possible to create a wrapper around the cache to handle the zero-capacity case and expose the desired APIs.
WDYT?

from lru-rs.

behzadnouri avatar behzadnouri commented on September 27, 2024

If you wish to support a cache with zero capacity, you will need to return an Option whenever you call get_or_insert or get_mut_or_insert

Not necessarily.
You can implement:

enum ValueWrapper<'a, V> {
    Borrowed(&'a V),
    Owned(V),
}

So that get_or_insert will return ValueWrapper instead, and the ValueWrapper can implement Borrow:

impl<'a, V> Borrow<V> for ValueWrapper<'a, V> {
    fn borrow(&self) -> &V {
        match self {
            Self::Borrowed(v) => v,
            Self::Owned(v) => v,
        }
    }
}

so that effectively it is equivalent to &V.

I have implemented this idea in #177

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.