Macro declare_attrs

Source
macro_rules! declare_attrs {
    ($(
        $(#[$attr:meta])*
        $vis:vis attr $name:ident: $type:ty $(= $default:expr)?;
    )*) => { ... };
    (@single $(#[$attr:meta])* ; $vis:vis attr $name:ident: $type:ty = $default:expr;) => { ... };
    (@single $(#[$attr:meta])* ; $vis:vis attr $name:ident: $type:ty;) => { ... };
}
Expand description

Declares attribute keys using a lazy_static! style syntax.

§Syntax

declare_attrs! {
    /// Documentation for the key (default visibility).
    attr KEY_NAME: Type = default_value;

    /// Another key (default value is optional)
    pub attr ANOTHER_KEY: AnotherType;
}

§Arguments

  • Optional visibility modifier (pub, pub(crate), etc.)
  • attr keyword (required)
  • Key name (identifier)
  • Type of values this key can store
  • Optional default value

§Example

use std::time::Duration;

use hyperactor::attrs::Attrs;
use hyperactor::attrs::declare_attrs;

declare_attrs! {
    /// Timeout for RPC operations
    pub attr TIMEOUT: Duration = Duration::from_secs(30);

    /// Maximum number of retry attempts (no default specified)
    attr MAX_RETRIES: u32;
}

let mut attrs = Attrs::new();
assert_eq!(attrs.get(TIMEOUT), Some(&Duration::from_secs(30)));
attrs.set(MAX_RETRIES, 5);