pneumatic (1.1.0)
Installation
[registries.forgejo]
index = "sparse+ " # Sparse index
# index = " " # Git
[net]
git-fetch-with-cli = truecargo add pneumatic@1.1.0 --registry forgejoAbout this package
pneumatic
pneumatic provides static delegation from an enum to the single-field tuple variants it wraps.
use std::future::Future;
use pneumatic::{pneumatic_trait, Pneumatic};
#[pneumatic_trait]
trait Operation {
fn name(&self) -> String;
fn run(&self) -> impl Future<Output = String>;
}
#[derive(Pneumatic)]
#[pneumatic(Operation)]
enum Operations {
First(First),
Second(Second),
}
The trait attribute records method signatures in a hidden callback macro. The enum derive consumes that method list and creates a match over every variant. Methods returning impl Future<Output = T> become async fn methods returning T; each variant call is awaited inside its match arm.
The enum must use one-field tuple variants, and delegated methods must have a receiver with identifier-pattern arguments. Generic methods are not currently supported. Rust does not allow derive macros on traits, so traits use the pneumatic_trait attribute.
When the trait and enum live in different modules or files, import the trait module with #[macro_use] so the generated method metadata macro is visible to the enum module:
#[macro_use]
mod traits;
mod enums;
Run the example test with:
cargo test
Dependencies
| ID | Version |
|---|---|
| proc-macro2 | ^1 |
| quote | ^1 |
| syn | ^2 |
| futures | ^0.3 |