logo
Expand description

Tools for writing procedural macroses.

Module :: proc_macro_tools

experimental rust-status docs.rs Open in Gitpod discord

Tools for writing procedural macroses.

Sample

#[ cfg( feature = "use_std" ) ]
{
  use proc_macro_tools::*;

  let code = qt!( core::option::Option< i8, i16, i32, i64 > );
  let tree_type = syn::parse2::< syn::Type >( code ).unwrap();
  let got = type_parameters( &tree_type, 0..=2 );
  got.iter().for_each( | e | println!( "{}", qt!( #e ) ) );
  /* print :
    i8
    i16
    i32
  */
}

To add to your project

cargo add proc_macro_tools

Try out from the repository

git clone https://github.com/Wandalen/wTools
cd wTools
cd sample/rust/proc_macro_tools_trivial
cargo run

Modules

Determine kind of a container.

Dependencies of the module.

Exposed namespace of the module.

Analyze generic to provide more information than trivial syntax node.

Macro helpers.

Tait to getn name of an Item.

Parented namespace of the module.

Prelude to use essentials: use my_module::prelude::*.

Protected namespace of the module.

Quantifiers like Pair and Many.

Advanced syntax elements.

Macros

A type-macro that expands to the name of the Rust type representation of a given token.

Generate code only if feature::make is enabled.

Parse a set of curly braces and expose their content to subsequent parsers.

Parse a set of square brackets and expose their content to subsequent parsers.

Macro for diagnostics purpose to diagnose source code behind it and export it into a string.

Macro to export source code behind a syntax tree into a string.

Macro for diagnostics purpose to print both syntax tree and source code behind it without syntax tree.

Define a type that supports parsing and printing a given identifier as if it were a keyword.

Define a type that supports parsing and printing a multi-character symbol as if it were a punctuation token.

Variadic constructor.

Parse a set of parentheses and expose their content to subsequent parsers.

Parse the input TokenStream of a macro, triggering a compile error if the tokens fail to parse.

Quasi-quotation macro that accepts input like the quote! macro but uses type inference to figure out a return type for those tokens.

Quasi-quotation macro that accepts input like the quote! macro but uses type inference to figure out a return type for those tokens.

This macro is [parse_quote!] + quote_spanned!.

Macro to generate syn error either with span of a syntax tree element or with default one proc_macro2::Span::call_site().

Macro for diagnostics purpose to export both syntax tree and source code behind it into a string.

Macro for diagnostics purpose to print both syntax tree and source code behind it with syntax tree.

Type constructor to define tuple wrapping a given type.

Structs

Attribute which is inner.

Attribute which is outer.

Iterator for enumerable.

Iterator for enumerable.

Result of generics analyze.

Type constructor to wrap pair of the same type.

Type constructor to wrap a vector.

Parse as much elements as possible.

Type constructor to wrap two types into a tuple.

Parse as much elements as possible.

Type constructor to wrap a another type into a tuple.

Enums

Kind of container.

Traits

Reinterpret as array.

Marker saying how to parse several elements of such type in a row.

Reinterpret as slice.

Reinterpret as tuple.

Clone as array.

Clone as tuple.

Has length and indexed access.

To analyze generics.

Constructor without arguments.

Constructor with single argument.

Constructor with two arguments.

Constructor with three arguments.

Trait to get name of an syntax element.

Implementation of trait From to vectorize into/from.

Implementation of trait Into to vectorize into/from.

Functions

For attribute like #[former( default = 31 )] return key default and value 31, as well as syn::Meta as the last element of result tuple.

Return kind of container specified by type.

Return kind of container specified by type. Unlike type_container_kind it also understand optional types.

Return the specified number of parameters of the type.

Check is the rightmost item of path refering a type is specified type.

Type Definitions

Attribute and ident.

Input to a Syn parser function.

Result with syn::Error.