1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
|
# task-local-extensions
Provides a type-safe task-local container for arbitrary data keyed by types.
[](https://crates.io/crates/task-local-extensions)
[](https://docs.rs/task-local-extensions)
[](https://github.com/TrueLayer/task-local-extensions/actions)
[](https://coveralls.io/github/TrueLayer/task-local-extensions?branch=main)
## How to install
Add `task-local-extensions` to your dependencies
```toml
[dependencies]
# ...
task-local-extensions = "0.1.0"
```
## Usage
[`Extensions`](https://docs.rs/task-local-extensions/latest/task_local_extensions/struct.Extensions.html)
is a container that can store up to one value of each type, so you can insert and retrive values by
their type:
```rust
use task_local_extensions::Extensions;
let a: i64 = 3;
let mut ext = Extensions::new();
ext.insert(a);
assert_eq!(ext.get::<i64>(), Some(&3));
```
The crate also provides [`with_extensions`](https://docs.rs/task-local-extensions/latest/task_local_extensions/fn.with_extensions.html)
so you set an `Extensions` instance while running a given task:
```rust
use task_local_extensions::{get_local_item, set_local_item, with_extensions, Extensions};
async fn my_task() {
let a: i64 = get_local_item().await.unwrap(0);
let msg = format!("The value of a is: {}", a);
set_local_item(msg).await;
}
let a: i64 = 3;
let (out_ext, _) = with_extensions(Extensions::new().with(a), my_task()).await;
let msg = out_ext.get::<String>().unwrap();
assert_eq!(msg.as_str(), "The value of a is: 3");
```
#### License
<sup>
Licensed under either of <a href="LICENSE-APACHE">Apache License, Version
2.0</a> or <a href="LICENSE-MIT">MIT license</a> at your option.
</sup>
<br>
<sub>
Unless you explicitly state otherwise, any contribution intentionally submitted
for inclusion in the work by you, as defined in the Apache-2.0 license, shall be
dual licensed as above, without any additional terms or conditions.
</sub>
|