4 releases

0.1.4 Mar 9, 2023
0.1.3 Sep 6, 2022
0.1.2 Aug 30, 2022
0.1.1 Aug 31, 2021

#45 in Algorithms

Download history 74337/week @ 2023-12-12 56282/week @ 2023-12-19 26738/week @ 2023-12-26 62849/week @ 2024-01-02 81983/week @ 2024-01-09 97049/week @ 2024-01-16 110772/week @ 2024-01-23 114088/week @ 2024-01-30 113291/week @ 2024-02-06 119511/week @ 2024-02-13 122448/week @ 2024-02-20 132336/week @ 2024-02-27 126307/week @ 2024-03-05 141984/week @ 2024-03-12 129989/week @ 2024-03-19 115969/week @ 2024-03-26

535,991 downloads per month
Used in 210 crates (32 directly)

MIT/Apache

12KB
141 lines

task-local-extensions

Provides a type-safe task-local container for arbitrary data keyed by types.

Crates.io Docs.rs CI Coverage Status

How to install

Add task-local-extensions to your dependencies

[dependencies]
# ...
task-local-extensions = "0.1.0"

Usage

Extensions is a container that can store up to one value of each type, so you can insert and retrive values by their type:

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 so you set an Extensions instance while running a given task:

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

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.

Dependencies

~6KB