Trait SparseContainer

Source
pub trait SparseContainer: Sized {
    type Input;
    type Output;

    // Required methods
    fn get(&self, input: &Self::Input) -> Option<&Self::Output>;
    fn put(&mut self, input: Self::Input, output: Self::Output) -> &Self::Output;

    // Provided method
    fn has(&self, input: &Self::Input) -> bool { ... }
}
Expand description

A generic trait for anything that would like to be used in a [GenericCache], allowing easy extensibility using a container not covered by this library.

[HashCache] and [BTreeCache] are both just using GenericCache under the hood, by implementing this trait on HashMap and BTreeMap.

If this trait doesn’t quite fit with your container, you can also implement fully your own [FnCache], which requires a bit more work than using this trait, but gives you full generality. This is how [VecCache] is implemented, because it is not sparse, and must fill all earlier indices.

Required Associated Types§

Required Methods§

Source

fn get(&self, input: &Self::Input) -> Option<&Self::Output>

Returns the output associated with input, if it exists.

Source

fn put(&mut self, input: Self::Input, output: Self::Output) -> &Self::Output

Associate a new output with the key input, which can later be retrieved using Self::get

Provided Methods§

Source

fn has(&self, input: &Self::Input) -> bool

Returns true if the container is holding an output associated with input.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl<I, O> SparseContainer for BTreeMap<I, O>
where I: Ord,

Source§

type Input = I

Source§

type Output = O

Source§

fn has(&self, input: &Self::Input) -> bool

Source§

fn get(&self, input: &Self::Input) -> Option<&Self::Output>

Source§

fn put(&mut self, input: Self::Input, output: Self::Output) -> &Self::Output

Source§

impl<I, O, S> SparseContainer for HashMap<I, O, S>
where I: Eq + Hash, S: BuildHasher,

Source§

type Input = I

Source§

type Output = O

Source§

fn has(&self, input: &I) -> bool

Source§

fn get(&self, input: &I) -> Option<&O>

Source§

fn put(&mut self, input: I, output: O) -> &O

Implementors§