tomplate_eager!() { /* proc-macro */ }Expand description
Eagerly expand tomplate! and concat! macros within a token stream.
This macro solves the problem where outer macros expect string literals but receive unexpanded macro calls. It walks the token tree and expands inner macros first, allowing seamless integration with other macro systems.
§Problem It Solves
Many macros (like sqlx::query!) require string literals as arguments.
They cannot accept unexpanded macro calls:
ⓘ
// ❌ This fails - sqlx::query! sees the macro call, not the string
sqlx::query!(tomplate!("select_user", id = user_id))
.fetch_one(&pool)
.await?;§Solution
tomplate_eager! pre-expands the inner macros:
ⓘ
// ✅ This works - tomplate! is expanded first, then passed to sqlx
tomplate_eager! {
sqlx::query!(tomplate!("select_user", id = user_id))
.fetch_one(&pool)
.await?
}§Supported Inner Macros
tomplate!- Expands template macrosconcat!- Expands string concatenation
§Examples
§With SQL Query Builders
ⓘ
// Using with sqlx
tomplate_eager! {
let user = sqlx::query_as!(
User,
tomplate!("get_user_by_id",
fields = "id, name, email",
table = "users"
),
user_id
)
.fetch_one(&pool)
.await?;
}§With String Concatenation
ⓘ
tomplate_eager! {
const FULL_QUERY: &str = concat!(
tomplate!("select_part"),
" UNION ALL ",
tomplate!("select_other_part")
);
}§Multiple Expansions
ⓘ
tomplate_eager! {
// Multiple statements with macro expansions
let query1 = sqlx::query!(tomplate!("query1"))
.fetch_all(&pool)
.await?;
let query2 = sqlx::query!(tomplate!("query2"))
.fetch_optional(&pool)
.await?;
}§How It Works
- Recursively walks through the provided token stream
- Finds any
tomplate!orconcat!invocations - Evaluates them at compile time
- Replaces them with their resulting string literals
- Returns the modified token stream
All processing happens at compile time with zero runtime overhead.