Expand description
Attribute dictionary for type-safe, heterogeneous key-value storage with serde support.
This module provides Attrs
, a type-safe dictionary that can store heterogeneous values
and serialize/deserialize them using serde. All stored values must implement
Serialize + DeserializeOwned
to ensure the entire dictionary can be serialized.
Keys are automatically registered at compile time using the declare_attrs!
macro and the
inventory crate, eliminating the need for manual registry management.
§Basic Usage
use std::time::Duration;
use hyperactor::attrs::Attrs;
use hyperactor::attrs::declare_attrs;
// Declare keys with their associated types
declare_attrs! {
/// Request timeout
attr TIMEOUT: Duration;
/// Maximum retry count
attr MAX_RETRIES: u32 = 3; // with default value
}
let mut attrs = Attrs::new();
attrs.set(TIMEOUT, Duration::from_secs(30));
assert_eq!(attrs.get(TIMEOUT), Some(&Duration::from_secs(30)));
assert_eq!(attrs.get(MAX_RETRIES), Some(&3));
§Serialization
Attrs
can be serialized to and deserialized automatically:
use std::time::Duration;
use hyperactor::attrs::Attrs;
use hyperactor::attrs::declare_attrs;
declare_attrs! {
/// Request timeout
pub attr TIMEOUT: Duration;
}
let mut attrs = Attrs::new();
attrs.set(TIMEOUT, Duration::from_secs(30));
// Serialize to JSON
let json = serde_json::to_string(&attrs).unwrap();
// Deserialize from JSON (no manual registry needed!)
let deserialized: Attrs = serde_json::from_str(&json).unwrap();
assert_eq!(deserialized.get(TIMEOUT), Some(&Duration::from_secs(30)));
Macros§
- declare_
attrs - Declares attribute keys using a lazy_static! style syntax.