This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
impl Fold for StmtCounter { | |
fn fold_stmt(&mut self, stmt_old: Stmt) -> Stmt { | |
let stmt_middle = syn::fold::fold_stmt(self, stmt_old); | |
println!("stmt #{}: {}", self.count, quote!(#stmt_middle)); | |
self.count += 1; | |
if quote!(#stmt_middle).to_string().contains("galaxy") { | |
parse_quote!(println!("hello universe");) | |
} else { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
impl Fold for StmtCounter { | |
fn fold_stmt(&mut self, stmt_old: Stmt) -> Stmt { | |
let stmt_middle = syn::fold::fold_stmt(self, stmt_old); | |
// your code goes | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct StmtCounter { | |
count: usize, | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn to_item_fn(&self) -> ItemFn { | |
ItemFn { | |
sig: Signature { | |
generics: self.to_generics(), | |
inputs: self.fn_args.clone(), | |
..self.old_fn.sig.clone() | |
}, | |
block: Box::new(Block { | |
stmts: self.stmts.clone(), | |
..*self.old_fn.block.clone() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use syn::{parse_quote, punctuated::Punctuated, token::Comma, token::Lt, WherePredicate}; | |
fn main() { | |
let _lt: Lt = parse_quote!(<); | |
let _where_list: Punctuated<WherePredicate, Comma> = parse_quote!(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn ident_to_stmt(&self, name: &Ident) -> Stmt { | |
match &self { | |
Special::AnyArray | Special::AnyString | Special::AnyPath => { | |
parse_quote! { | |
let #name = #name.as_ref(); | |
} | |
} | |
Special::AnyIter => { | |
parse_quote! { | |
let #name = #name.into_iter(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Even if the where clause is None, we still need to return an empty Punctuated | |
fn extract_where_predicates(item_fn: &ItemFn) -> Punctuated<WherePredicate, Comma> { | |
if let Some(WhereClause { predicates, .. }) = &item_fn.sig.generics.where_clause { | |
predicates.clone() | |
} else { | |
parse_quote!() | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn assert_tokens_eq(expected: &TokenStream, actual: &TokenStream) { | |
let expected = expected.to_string(); | |
let actual = actual.to_string(); | |
if expected != actual { | |
println!( | |
"{}", | |
colored_diff::PrettyDifference { | |
expected: &expected, | |
actual: &actual, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[test] | |
fn one_input() { | |
let before = quote! { | |
fn any_str_len(s: AnyString) -> usize | |
{ | |
s.len() | |
} | |
}; | |
let expected = quote! { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
To | From | Method | |
---|---|---|---|
string of code | literal code | std::stringify!{} | |
string of code | syntax tree | !quote(#syntax_tree).to_string() | |
string of code | TokenStream | .to_string() | |
string of syntax | literal code | format!("{:?}",parse2::<SynType>(quote! {...}).expect(...) | |
string of syntax | syntax tree | format!("{:?}"),…), format!("{:#?}"),…) | |
string of tokens | literal code | format!("{:?}",quote! {...}) | |
string of tokens | TokenStream | format!("{:?}"),…), format!("{:#?}"),…) | |
syn::Error | TokenStream | .to_compile_error() [see Rule #7] | |
syntax tree | literal code | parse_quote!(…) |