cachingmap 0.2.1

A caching Hashmap accepting new entries through interior-mutability
Documentation
  • Coverage
  • 69.23%
    9 out of 13 items documented2 out of 10 items with examples
  • Size
  • Source code size: 11.52 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 2.29 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Links
  • aurelien-naldi/cachingmap
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • aurelien-naldi

Caching map

This crate provides a map designed for caching intermediate results by accepting new entries while immutable. It provides simple immutable references to the cached values which remain valid while new entries are added. To return lightweight references without a guard or any encapsulation, it uses an UnsafeCell and short pieces of unsafe code. The returned references are guaranted to remain safe as existing entries can NOT be removed without proper mutable access. Values are boxed to ensure that pointers remain stable when the HashMap is extended.

⚠️ Earlier versions did not use boxed values, old returned references could become invalid.

⚠️ This map is NOT thread-safe, but may be accepted in multi-threaded code (untested).

The main objective is to include this map in a larger data structure and to use it to store the result of some computations based on other fields. Individual entries may then need to be cleared when some fields are modified to ensure that the next returned result remain valid.

The current version is a proof of concept, with some potential room for improvement.

  • The code is short and seems valid in single-threaded context, but uses unsafe code
  • It is probably not thread safe. If it compiles in multi-threaded context, it should be blocked. A thread-safe extension could be useful.
  • It uses an internal HashMap, but could be extended to other Indexable backend.