#static #tls #state #thread-local #global

state

A library for safe and effortless global and thread-local state management

11 releases

0.4.1 Apr 27, 2018
0.4.0 Jan 23, 2018
0.3.3 Jan 6, 2018
0.3.2 Sep 17, 2017
0.2.0 Jan 21, 2017

#70 in Network programming

Download history 6085/week @ 2019-12-10 5678/week @ 2019-12-17 4059/week @ 2019-12-24 4266/week @ 2019-12-31 6212/week @ 2020-01-07 7391/week @ 2020-01-14 6158/week @ 2020-01-21 7690/week @ 2020-01-28 7299/week @ 2020-02-04 6749/week @ 2020-02-11 6834/week @ 2020-02-18 8064/week @ 2020-02-25 6619/week @ 2020-03-03 6845/week @ 2020-03-10 7786/week @ 2020-03-17 6140/week @ 2020-03-24

23,830 downloads per month
Used in 153 crates (9 directly)

MIT/Apache

42KB
361 lines

State

A Rust library for safe and effortless global and thread-local state management.

extern crate state;

static GLOBAL: state::Storage<u32> = state::Storage::new();

GLOBAL.set(42);
assert_eq!(*GLOBAL.get(), 42);

This library can be used to easily implement:

  • Global Singletons
  • Global or Local Cached Values
  • Thread-Local Thunks
  • Dynamic Thread-Local Data
  • Typed Containers

See the documentation for more.

Usage

Include state in your Cargo.toml [dependencies]:

[dependencies]
state = "0.4"

Thread-local state management is not enabled by default. You can enable it via the tls feature:

[dependencies]
state = { version = "0.4", features = ["tls"] }

All constructors may be made const by enabling the const_fn feature:

[dependencies]
state = { version = "0.4", features = ["const_fn"] }

This will require Rust nightly due to the instability of the const_fn feature. Ensure that it is enabled by adding the following to your top-level crate attributes:

#![feature(const_fn)]

Testing

This library is extensively tested. Tests can be found in tests/main.rs. You can run the tests on Rust nightly with cargo test --all-features.

License

State is licensed under either of the following, at your option:

Dependencies

~43KB