[][src]Macro genco::quote

quote!() { /* proc-macro */ }

Quotes the specified expression as a stream of tokens for use with genco.

Mechanisms

  • Elements are interpolated using #, so to include the variable test, you could write #test. Returned elements must implement FormatTokens.
  • Inline statements can be evaluated using #(<stmt>), or #{<stmt>}, or #[<stmt>]. In effect, anything that counts as a group in Rust. For example: #("test".quoted()) can be used to quote a string.
  • The register functionality of Tokens is available by prefixing an expression with @ as @<stmt>. For example: @only_imports.
  • # can be escaped by repeating it twice in case it's needed in the target language. So ## would produce a single #.
  • Expressions can be repeated. It is then expected that they evaluate to an iterator. Expressions are repeated by adding the <token>* suffix. The will then be used as a separator between each element, and a spacing will be added after it. For example: #(var),* will treat var as an iterator and add , between each element.

Examples

#![feature(proc_macro_hygiene)]

use genco::rust::imported;
use genco::{quote, Rust, Tokens};

// Import the LittleEndian item, without referencing it through the last
// module component it is part of.
let little_endian = imported("byteorder", "LittleEndian").qualified();
let big_endian = imported("byteorder", "BigEndian");

// This is a trait, so only import it into the scope (unless we intent to
// implement it).
let write_bytes_ext = imported("byteorder", "WriteBytesExt").alias("_");

let tokens: Tokens<Rust> = quote! {
    @write_bytes_ext

    let mut wtr = vec![];
    wtr.write_u16::<#little_endian>(517).unwrap();
    wtr.write_u16::<#big_endian>(768).unwrap();
    assert_eq!(wtr, vec![5, 2, 3, 0]);
};

println!("{}", tokens.to_file_string().unwrap());