quote!() { /* proc-macro */ }
Expand description
Quotes the specified expression as a stream of tokens for use with genco.
§Mechanisms
- Elements are interpolated using
#
, so to include the variabletest
, you could write#test
. Returned elements must implementFormatTokens
. - 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
. #
and@
can be escaped by repeating it twice in case it’s needed in the target language. So##
would produce a single#
, and@@
would produce a single@
.
§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());