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
AttrValue
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)));
§Meta attributes
An attribute can be assigned a set of attribute values,
associated with the attribute key. These are specified by
@-annotations in the declare_attrs!
macro:
use std::time::Duration;
use hyperactor::attrs::Attrs;
use hyperactor::attrs::declare_attrs;
declare_attrs! {
/// Is experimental?
pub attr EXPERIMENTAL: bool;
/// Request timeout
@meta(EXPERIMENTAL = true)
pub attr TIMEOUT: Duration;
}
assert!(TIMEOUT.attrs().get(EXPERIMENTAL).unwrap());
Meta attributes can be used to provide more generic functionality on top of the basic attributes. For example, a library can use meta-attributes to specify the behavior of an attribute.
Macros§
- declare_
attrs - Declares attribute keys using a lazy_static! style syntax.
Structs§
- Attrs
- A heterogeneous, strongly-typed attribute dictionary with serialization support.
- Key
- A typed key for the attribute dictionary.