task_local_extensions/lib.rs
1//! A type map for storing data of arbritrary type.
2//!
3//! # Extensions
4//! [`Extensions`] is a container that can store up to one value of each type, so you can insert and retrive values by
5//! their type:
6//!
7//! ```
8//! use task_local_extensions::Extensions;
9//!
10//! let a: i64 = 3;
11//! let mut ext = Extensions::new();
12//! extensions.insert(a);
13//! assert_eq!(ext.get::<i64>(), Some(&3));
14//! ```
15//!
16//! # Task Local Extensions
17//! The crate also provides [`with_extensions`] so you set an [`Extensions`] instance while running a given task:
18//!
19//! ```
20//! use task_local_extensions::{get_local_item, set_local_item, with_extensions, Extensions};
21//!
22//! async fn my_task() {
23//! let a: i64 = get_local_item().await.unwrap(0);
24//! let msg = format!("The value of a is: {}", a);
25//! set_local_item(msg).await;
26//! }
27//!
28//! let a: i64 = 3;
29//! let (out_ext, _) = with_extensions(Extensions::new().with(a), my_task()).await;
30//! let msg = out_ext.get::<String>().unwrap();
31//! assert_eq!(msg.as_str(), "The value of a is: 3");
32//! ```
33
34mod extensions;
35mod task_local;
36
37pub use extensions::*;
38pub use task_local::*;