Skip to main content

preprocessor/
lib.rs

1//! # preprocessor
2//!
3//! Compile-time computation macros for Rust. Analyzes code for computable
4//! sub-expressions and evaluates them at compile time, so the final binary
5//! contains only the results.
6//!
7//! ## Macros
8//!
9//! | Macro | Scope | Description |
10//! |---|---|---|
11//! | `#[preprocessor::optimize]` | Function | Optimizes all evaluable expressions in a function body |
12//! | `preprocessor::op!(...)` | Expression | Evaluates a single expression at compile time |
13//!
14//! ## Features
15//!
16//! | Feature | Description |
17//! |---|---|
18//! | `disabled` | Disables all compile-time optimization; macros become transparent passthrough |
19//!
20//! ## Example
21//!
22//! ```rust
23//! use preprocessor::op;
24//!
25//! // Compile-time evaluation
26//! let result = op!(1 + 2 * 3); // → 7
27//!
28//! // With free variables — compile error
29//! // let x = 5;
30//! // let y = op!(x + 1); // ERROR: cannot evaluate at compile time
31//! ```
32//!
33//! ```rust,ignore
34//! use preprocessor::optimize;
35//!
36//! #[optimize]
37//! fn compute() -> i32 {
38//!     let a = 1 + 2; // → 3
39//!     let b = 4 * 5; // → 20
40//!     a + b
41//! }
42//! ```
43
44#[cfg(not(feature = "disabled"))]
45pub use preprocessor_derive::{op, optimize, prelude};
46
47/// When `disabled` feature is enabled, `op!` becomes transparent passthrough.
48#[cfg(feature = "disabled")]
49#[macro_export]
50macro_rules! op {
51    ($($tt:tt)*) => {
52        $($tt)*
53    };
54}
55
56/// When `disabled` feature is enabled, `#[optimize]` becomes a no-op passthrough.
57/// Uses a declarative macro wrapper that re-emits the function unchanged.
58#[cfg(feature = "disabled")]
59pub use preprocessor_derive::optimize;