quantum-pulse-macros
Procedural macros for the quantum-pulse profiling library.
Overview
This crate provides derive macros to automatically implement profiling traits from the quantum-pulse library, reducing boilerplate code and making it easier to integrate profiling into your applications.
Features
#[derive(Operation)]- Automatically implements theOperationtrait for enums- Zero-cost abstractions - Generated code is optimized away when profiling is disabled
- Flexible categorization - Define custom categories with descriptions
- Type-safe - All category definitions are checked at compile time
Installation
This crate is typically used as a dependency of quantum-pulse. Add to your Cargo.toml:
[]
= { = "0.1", = ["macros"] }
Usage
Basic Example
use ;
Category Attributes
The #[category(...)] attribute supports the following parameters:
name- The name of the category (optional, defaults to variant name)description- A description of the category (optional, defaults to category name)
Important Behavior
When multiple enum variants use the same category name:
- Only one category struct is generated per unique category name
- The first description wins - The first
descriptionencountered for a category name is used - Subsequent descriptions are ignored - If a category already has a description, later ones are ignored
- Description upgrade - If the first variant with a category name has no description (defaulting to the name), and a later variant provides one, the description is updated
Example with Shared Categories
Enum Variants with Data
The macro supports all enum variant types:
Special Characters in Category Names
Category names can contain special characters, which are automatically sanitized for internal use:
How It Works
The #[derive(Operation)] macro:
- Parses the enum and its
#[category]attributes - Collects unique categories and their descriptions
- Generates a hidden struct for each unique category that implements the
Categorytrait - Implements the
Operationtrait for the enum with aget_category()method that returns the appropriate category
Generated structs are prefixed with the enum name to avoid naming conflicts when multiple enums use the same category names.
Performance
The generated code has minimal runtime overhead:
- Category structs are zero-sized types (ZSTs)
- The
get_category()method is a simple match expression - All generated code can be inlined by the compiler
- When profiling is disabled (default), everything compiles to nothing
License
This crate is part of the quantum-pulse project and is dual-licensed under MIT OR Apache-2.0.