Skip to main content

litext

Macro litext 

Source
macro_rules! litext {
    (@inner $iter:expr, $T1:tt) => { ... };
    (@inner $iter:expr, $T1:tt $s1:tt $T2:tt) => { ... };
    (@inner $iter:expr, $T1:tt $s1:tt $T2:tt $s2:tt $T3:tt) => { ... };
    (@inner $iter:expr, $T1:tt $s1:tt $T2:tt $s2:tt $T3:tt $s3:tt $T4:tt) => { ... };
    (@inner $iter:expr, $T1:tt $s1:tt $T2:tt $s2:tt $T3:tt $s3:tt $T4:tt $s4:tt $T5:tt) => { ... };
    (@inner $iter:expr, $T1:tt $s1:tt $T2:tt $s2:tt $T3:tt $s3:tt $T4:tt $s4:tt $T5:tt $s5:tt $T6:tt) => { ... };
    (@inner $iter:expr, $T1:tt $s1:tt $T2:tt $s2:tt $T3:tt $s3:tt $T4:tt $s4:tt $T5:tt $s5:tt $T6:tt $s6:tt $T7:tt) => { ... };
    (@inner $iter:expr, $T1:tt $s1:tt $T2:tt $s2:tt $T3:tt $s3:tt $T4:tt $s4:tt $T5:tt $s5:tt $T6:tt $s6:tt $T7:tt $s7:tt $T8:tt) => { ... };
    (@inner $iter:expr, $T1:tt $s1:tt $T2:tt $s2:tt $T3:tt $s3:tt $T4:tt $s4:tt $T5:tt $s5:tt $T6:tt $s6:tt $T7:tt $s7:tt $T8:tt $s8:tt $T9:tt) => { ... };
    (@inner $iter:expr, $T1:tt $s1:tt $T2:tt $s2:tt $T3:tt $s3:tt $T4:tt $s4:tt $T5:tt $s5:tt $T6:tt $s6:tt $T7:tt $s7:tt $T8:tt $s8:tt $T9:tt $s9:tt $T10:tt) => { ... };
    (@inner $iter:expr, $T1:tt $s1:tt $T2:tt $s2:tt $T3:tt $s3:tt $T4:tt $s4:tt $T5:tt $s5:tt $T6:tt $s6:tt $T7:tt $s7:tt $T8:tt $s8:tt $T9:tt $s9:tt $T10:tt $s10:tt $T11:tt) => { ... };
    (@inner $iter:expr, $T1:tt $s1:tt $T2:tt $s2:tt $T3:tt $s3:tt $T4:tt $s4:tt $T5:tt $s5:tt $T6:tt $s6:tt $T7:tt $s7:tt $T8:tt $s8:tt $T9:tt $s9:tt $T10:tt $s10:tt $T11:tt $s11:tt $T12:tt) => { ... };
    ($input:ident as ($T1:tt)) => { ... };
    ($input:ident as ($T1:tt $s1:tt $T2:tt)) => { ... };
    ($input:ident as ($T1:tt $s1:tt $T2:tt $s2:tt $T3:tt)) => { ... };
    ($input:ident as ($T1:tt $s1:tt $T2:tt $s2:tt $T3:tt $s3:tt $T4:tt)) => { ... };
    ($input:ident as ($T1:tt $s1:tt $T2:tt $s2:tt $T3:tt $s3:tt $T4:tt $s4:tt $T5:tt)) => { ... };
    ($input:ident as ($T1:tt $s1:tt $T2:tt $s2:tt $T3:tt $s3:tt $T4:tt $s4:tt $T5:tt $s5:tt $T6:tt)) => { ... };
    ($input:ident as ($T1:tt $s1:tt $T2:tt $s2:tt $T3:tt $s3:tt $T4:tt $s4:tt $T5:tt $s5:tt $T6:tt $s6:tt $T7:tt)) => { ... };
    ($input:ident as ($T1:tt $s1:tt $T2:tt $s2:tt $T3:tt $s3:tt $T4:tt $s4:tt $T5:tt $s5:tt $T6:tt $s6:tt $T7:tt $s7:tt $T8:tt)) => { ... };
    ($input:ident as ($T1:tt $s1:tt $T2:tt $s2:tt $T3:tt $s3:tt $T4:tt $s4:tt $T5:tt $s5:tt $T6:tt $s6:tt $T7:tt $s7:tt $T8:tt $s8:tt $T9:tt)) => { ... };
    ($input:ident as ($T1:tt $s1:tt $T2:tt $s2:tt $T3:tt $s3:tt $T4:tt $s4:tt $T5:tt $s5:tt $T6:tt $s6:tt $T7:tt $s7:tt $T8:tt $s8:tt $T9:tt $s9:tt $T10:tt)) => { ... };
    ($input:ident as ($T1:tt $s1:tt $T2:tt $s2:tt $T3:tt $s3:tt $T4:tt $s4:tt $T5:tt $s5:tt $T6:tt $s6:tt $T7:tt $s7:tt $T8:tt $s8:tt $T9:tt $s9:tt $T10:tt $s10:tt $T11:tt)) => { ... };
    ($input:ident as ($T1:tt $s1:tt $T2:tt $s2:tt $T3:tt $s3:tt $T4:tt $s4:tt $T5:tt $s5:tt $T6:tt $s6:tt $T7:tt $s7:tt $T8:tt $s8:tt $T9:tt $s9:tt $T10:tt $s10:tt $T11:tt $s11:tt $T12:tt)) => { ... };
    (try $input:ident as ($T1:tt)) => { ... };
    (try $input:ident as ($T1:tt $s1:tt $T2:tt)) => { ... };
    (try $input:ident as ($T1:tt $s1:tt $T2:tt $s2:tt $T3:tt)) => { ... };
    (try $input:ident as ($T1:tt $s1:tt $T2:tt $s2:tt $T3:tt $s3:tt $T4:tt)) => { ... };
    (try $input:ident as ($T1:tt $s1:tt $T2:tt $s2:tt $T3:tt $s3:tt $T4:tt $s4:tt $T5:tt)) => { ... };
    (try $input:ident as ($T1:tt $s1:tt $T2:tt $s2:tt $T3:tt $s3:tt $T4:tt $s4:tt $T5:tt $s5:tt $T6:tt)) => { ... };
    (try $input:ident as ($T1:tt $s1:tt $T2:tt $s2:tt $T3:tt $s3:tt $T4:tt $s4:tt $T5:tt $s5:tt $T6:tt $s6:tt $T7:tt)) => { ... };
    (try $input:ident as ($T1:tt $s1:tt $T2:tt $s2:tt $T3:tt $s3:tt $T4:tt $s4:tt $T5:tt $s5:tt $T6:tt $s6:tt $T7:tt $s7:tt $T8:tt)) => { ... };
    (try $input:ident as ($T1:tt $s1:tt $T2:tt $s2:tt $T3:tt $s3:tt $T4:tt $s4:tt $T5:tt $s5:tt $T6:tt $s6:tt $T7:tt $s7:tt $T8:tt $s8:tt $T9:tt)) => { ... };
    (try $input:ident as ($T1:tt $s1:tt $T2:tt $s2:tt $T3:tt $s3:tt $T4:tt $s4:tt $T5:tt $s5:tt $T6:tt $s6:tt $T7:tt $s7:tt $T8:tt $s8:tt $T9:tt $s9:tt $T10:tt)) => { ... };
    (try $input:ident as ($T1:tt $s1:tt $T2:tt $s2:tt $T3:tt $s3:tt $T4:tt $s4:tt $T5:tt $s5:tt $T6:tt $s6:tt $T7:tt $s7:tt $T8:tt $s8:tt $T9:tt $s9:tt $T10:tt $s10:tt $T11:tt)) => { ... };
    (try $input:ident as ($T1:tt $s1:tt $T2:tt $s2:tt $T3:tt $s3:tt $T4:tt $s4:tt $T5:tt $s5:tt $T6:tt $s6:tt $T7:tt $s7:tt $T8:tt $s8:tt $T9:tt $s9:tt $T10:tt $s10:tt $T11:tt $s11:tt $T12:tt)) => { ... };
    ($input:ident $(as String)?) => { ... };
    (try $input:ident) => { ... };
    ($input:ident as $T:ty) => { ... };
    (try $input:ident as $T:ty) => { ... };
}
Expand description

The whole point.

Extracts typed literal values from a TokenStream with concise syntax.

This is the primary entry point for most proc-macro use. It wraps extract for single-value extraction and uses an iterator-based approach for multi-value (variadic) extraction.

§Syntax

litext!(input)                        // Extract as String, return early on error
litext!(input as String)              // Same, explicit type
litext!(input as T)                   // Extract as type T, return early on error
litext!(try input)                    // Extract as String, return Result
litext!(try input as T)              // Extract as T, return Result

// Multi-extraction: types separated by a punctuation separator
litext!(input as (T1 sep T2))         // Extract two values, return early on error
litext!(try input as (T1 sep T2))     // Same, return Result<(T1, T2), _>

Up to 12 values can be extracted per call.

§Arguments

  • input: a bare variable name (ident) holding the TokenStream
  • as T: the target type; defaults to String
  • try: when present, returns Result<T, TokenStream> instead of returning early
  • as (T1 sep T2 ...): extracts multiple values separated by sep in the token stream

§Multi-Extraction Notes

Type arguments in tuple position must be single-token identifiers. LitInt<u8> spans multiple tokens and cannot be used directly. Use LitInt (defaults to i32) or a plain integer type like i32 instead.

The separator sep is any single- or two-character punctuation token (,, ;, |, ->, =>, ::, .., etc.) and must match the actual separator in the input TokenStream. Two-character separators are written as two adjacent token-tree slots in the macro call, e.g. (String -> u32).

§Examples

use litext::{litext, LitStr, TokenStream};

fn my_string_macro(input: TokenStream) -> TokenStream {
    let text: String = litext!(input);
    // or: let text: String = litext!(input as String);
    quote::quote! { #text }
}

fn my_int_macro(input: TokenStream) -> TokenStream {
    let num: i32 = litext!(input as i32);
    quote::quote! { #num }
}

fn my_try_macro(input: TokenStream) -> TokenStream {
    let result: Result<String, _> = litext!(try input);
    match result {
        Ok(text) => quote::quote! { #text },
        Err(e) => e,
    }
}

fn my_multi_macro(input: TokenStream) -> TokenStream {
    // input must be: "name" , 42
    let (name, count): (String, u32) = litext!(input as (String , u32));
    quote::quote! { /* use name and count */ }
}

§See Also

  • extract for extracting a single value directly without the macro
  • FromLit for implementing custom literal types