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 my_proc_macros_lib::comp; | |
pub fn test_list_compreh() { | |
let vec_of_vecs = vec![vec![1, 2, 3], vec![4, 5, 6]]; | |
let result = comp![x for vec in vec_of_vecs for x in vec if x > 1 && x < 6].collect::<Vec<_>>(); | |
println!("{:?}", result); | |
assert_eq!(result, [2, 3, 4, 5]); | |
} |
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
let vec_of_vecs = vec![vec![1, 2, 3], vec![4, 5, 6]]; | |
let result = comp![x for vec in vec_of_vecs for x in vec if x > 1].collect::<Vec<_>>(); |
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 ToTokens for Comprehension { | |
fn to_tokens(&self, tokens: &mut TokenStream2) { | |
let all_for_if_clauses = | |
std::iter::once(&self.for_if_clause).chain(&self.additional_for_if_clauses); | |
let mut innermost_to_outermost = all_for_if_clauses.rev(); | |
let mut output = { | |
// innermost is a special case--here we do the mapping | |
let innermost = innermost_to_outermost | |
.next() |
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 Condition(Expr); | |
impl Parse for Condition { | |
fn parse(input: ParseStream) -> syn::Result<Self> { | |
_ = input.parse::<Token![if]>()?; | |
input.parse().map(Self) | |
} | |
} | |
impl ToTokens for Condition { |
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 ForIfClause { | |
pattern: Pat, | |
sequence: Expr, | |
conditions: Vec<Condition>, | |
} | |
impl Parse for ForIfClause { | |
fn parse(input: ParseStream) -> syn::Result<Self> { | |
_ = input.parse::<Token![for]>()?; | |
let pattern = Pat::parse_single(input)?; |
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
pub struct Comprehension { | |
pub mapping: Mapping, | |
pub for_if_clause: ForIfClause, | |
pub additional_for_if_clauses: Vec<ForIfClause>, | |
} | |
impl Parse for Comprehension { | |
fn parse(input: ParseStream) -> syn::Result<Self> { | |
Ok(Self { | |
mapping: input.parse()?, |
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 proc_macro2::TokenStream as TokenStream2; | |
use quote::{quote, ToTokens}; | |
use syn::{ | |
parse::{Parse, ParseStream}, | |
parse_macro_input, Expr, Pat, Token, | |
}; | |
pub struct Comprehension { | |
pub mapping: Mapping, | |
pub for_if_clause: ForIfClause, |
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
xs = [-1, 2, 2, 3, 4] | |
out = [x * 2 for x in xs if x > 0] | |
# out = [-1, 4, 4, 6, 8] |
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
create_custom_function_and_print!(hello: "Hello, world!"); |
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
Entering compute | |
Return value from compute: 20 | |
Exiting compute (elapsed: 369µs) |
NewerOlder