#tokens #store #values #retrieve #type #module #token-based

token_store

A token-based store for arbitrary values

3 releases

Uses old Rust 2015

0.1.2 Oct 12, 2017
0.1.1 Oct 11, 2017
0.1.0 Oct 10, 2017

#31 in #retrieve

Download history 451/week @ 2024-07-21 436/week @ 2024-07-28 323/week @ 2024-08-04 822/week @ 2024-08-11 417/week @ 2024-08-18 560/week @ 2024-08-25 509/week @ 2024-09-01 354/week @ 2024-09-08 392/week @ 2024-09-15 559/week @ 2024-09-22 415/week @ 2024-09-29 68/week @ 2024-10-06 310/week @ 2024-10-13 279/week @ 2024-10-20 407/week @ 2024-10-27 332/week @ 2024-11-03

1,331 downloads per month

MIT license

19KB
289 lines

Build Status Coverage Status

Token Store

This crate provides a simple token-based store for arbitrary types.

What is it for?

This crate was actually part of wayland-rs initially and extracted. The reason for its existence is the way these crates are designed, there is strong separation of data vs logic.

This token_store works well in such configurations: you have a set of well-separated modules that need to share data but don't need to access it conccurently. And also, the modules are not necessarily aware of each other (and so you can not really use a big fixed struct for storing all the shared data.

Using token_store, at initialization each module will store the value it needs in the store, and keep the tokens internally. It can optionnaly provide to the outside world tokens to access values that are to be shared.

Then, when each module needs to do its work, it just needs a &mut Store and can retrieve with its tokens the data it needs to work on, independently of what other modules may have stored in.

How do I use it?

use token_store::Store;

// create a store
let mut store = Store::new();

// insert some things in it, you are given tokens
let token1 = store.insert(42);

// you can store any type as log as it is `Any + 'static`
let token2 = store.insert(String::from("I like trains"));

// the tokens keep the information of the store type,
// as such you don't need any annotation to retrieve a value:
store.get_mut(&token2).push_str(", and cars too!");

The retrieved tokens can be cloned and shared as you like between various parts of your code.

Documentation

The documentation for the master branch is available online.

The documentation for the releases can be found on docs.rs:

No runtime deps