https://github.com/truelayer/task-local-extensions
Task-local container for arbitrary data
https://github.com/truelayer/task-local-extensions
Last synced: 6 months ago
JSON representation
Task-local container for arbitrary data
- Host: GitHub
- URL: https://github.com/truelayer/task-local-extensions
- Owner: TrueLayer
- License: apache-2.0
- Created: 2021-08-10T14:25:32.000Z (almost 5 years ago)
- Default Branch: main
- Last Pushed: 2024-02-10T11:16:09.000Z (over 2 years ago)
- Last Synced: 2024-12-18T05:39:04.205Z (over 1 year ago)
- Language: Rust
- Size: 28.3 KB
- Stars: 4
- Watchers: 7
- Forks: 3
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE-APACHE
- Code of conduct: CODE_OF_CONDUCT.md
- Codeowners: .github/CODEOWNERS
Awesome Lists containing this project
README
# 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::(), 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::().unwrap();
assert_eq!(msg.as_str(), "The value of a is: 3");
```
#### License
Licensed under either of Apache License, Version
2.0 or MIT license at your option.
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.