Skip to content

Commit 64e4254

Browse files
更合理的重命名 __rusthension -> 后缀_comprehension
1 parent a02b03e commit 64e4254

10 files changed

+81
-81
lines changed

src/eager_evaluation.rs

+41-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ pub(crate) fn handle_nested_loops<'a>(
4343
need_to_shadow.push(iterable);
4444
quote! { &#iterable }
4545
}
46-
Expr::MethodCall(_) => match crate::is_iter(iterable) {
46+
Expr::MethodCall(_) => match is_iter(iterable) {
4747
true => quote! { #iterable },
4848
_ => panic!(
4949
"please ensure the first method call is iter(): \n{:#?}",
@@ -89,6 +89,46 @@ pub(crate) fn handle_nested_loops<'a>(
8989
nested_code
9090
}
9191

92+
struct IterMethodCallFinder {
93+
is_iter: bool,
94+
}
95+
96+
use syn::{ExprMethodCall, visit::Visit};
97+
impl<'ast> Visit<'ast> for IterMethodCallFinder {
98+
fn visit_expr_method_call(&mut self, node: &'ast ExprMethodCall) {
99+
match *node.receiver {
100+
syn::Expr::Path(_) | syn::Expr::Field(_) => {
101+
if node.method == "iter" {
102+
self.is_iter = true;
103+
}
104+
}
105+
_ => syn::visit::visit_expr(&mut *self, &node.receiver),
106+
}
107+
}
108+
}
109+
110+
fn is_iter(expr: &syn::Expr) -> bool {
111+
let mut finder = IterMethodCallFinder { is_iter: false };
112+
finder.visit_expr(expr);
113+
114+
finder.is_iter
115+
}
116+
117+
#[test]
118+
fn test_is_iter() {
119+
// 最右侧是iter方法
120+
let expr = syn::parse_quote!(some.method_1().method_2().iter());
121+
assert!(!is_iter(&expr));
122+
eprintln!("--------------------------------");
123+
// 最左侧是iter方法
124+
let expr = syn::parse_quote!(some.iter().method_3().method_4());
125+
assert!(is_iter(&expr));
126+
eprintln!("--------------------------------");
127+
128+
let expr = syn::parse_quote!(some.method_5().iter().method_6());
129+
assert!(!is_iter(&expr));
130+
}
131+
92132
#[cfg(test)]
93133
mod tests {
94134
use super::*;

src/eager_evaluation/b_tree_map.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ impl quote::ToTokens for BTreeMapComprehension {
2828

2929
let mut nested_code = match right_expr {
3030
None => quote! {
31-
__rusthension_b_tree_map.insert(#left_key, #left_value);
31+
__b_tree_map_comprehension.insert(#left_key, #left_value);
3232
},
3333
Some(MappingElse {
3434
conditions,
@@ -41,9 +41,9 @@ impl quote::ToTokens for BTreeMapComprehension {
4141

4242
quote! {
4343
if #conditions {
44-
__rusthension_b_tree_map.insert(#left_key, #left_value);
44+
__b_tree_map_comprehension.insert(#left_key, #left_value);
4545
} else {
46-
__rusthension_b_tree_map.insert(#else_key, #else_value);
46+
__b_tree_map_comprehension.insert(#else_key, #else_value);
4747
}
4848
}
4949
}
@@ -53,9 +53,9 @@ impl quote::ToTokens for BTreeMapComprehension {
5353
nested_code = quote! {
5454
{
5555
use ::std::collections::BTreeMap;
56-
let mut __rusthension_b_tree_map = BTreeMap::new();
56+
let mut __b_tree_map_comprehension = BTreeMap::new();
5757
#nested_code
58-
__rusthension_b_tree_map
58+
__b_tree_map_comprehension
5959
}
6060
};
6161

src/eager_evaluation/b_tree_set.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ impl quote::ToTokens for BTreeSetComprehension {
2828

2929
let mut nested_code = match right_expr {
3030
None => quote! {
31-
__rusthension_b_tree_set.insert(#left_key);
31+
__b_tree_set_comprehension.insert(#left_key);
3232
},
3333
Some(MappingElse {
3434
conditions,
@@ -41,9 +41,9 @@ impl quote::ToTokens for BTreeSetComprehension {
4141

4242
quote! {
4343
if #conditions {
44-
__rusthension_b_tree_set.insert(#left_key);
44+
__b_tree_set_comprehension.insert(#left_key);
4545
} else {
46-
__rusthension_b_tree_set.insert(#else_key);
46+
__b_tree_set_comprehension.insert(#else_key);
4747
}
4848
}
4949
}
@@ -53,9 +53,9 @@ impl quote::ToTokens for BTreeSetComprehension {
5353
nested_code = quote! {
5454
{
5555
use ::std::collections::BTreeSet;
56-
let mut __rusthension_b_tree_set = BTreeSet::new();
56+
let mut __b_tree_set_comprehension = BTreeSet::new();
5757
#nested_code
58-
__rusthension_b_tree_set
58+
__b_tree_set_comprehension
5959
}
6060
};
6161

src/eager_evaluation/binary_heap.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ impl quote::ToTokens for BinaryHeapComprehension {
2828

2929
let mut nested_code = match right_expr {
3030
None => quote! {
31-
__rusthension_binary_heap.push(#left_key);
31+
__binary_heap_comprehension.push(#left_key);
3232
},
3333
Some(MappingElse {
3434
conditions,
@@ -37,9 +37,9 @@ impl quote::ToTokens for BinaryHeapComprehension {
3737
}) => {
3838
quote! {
3939
if #conditions {
40-
__rusthension_binary_heap.push(#left_key);
40+
__binary_heap_comprehension.push(#left_key);
4141
} else {
42-
__rusthension_binary_heap.push(#else_key);
42+
__binary_heap_comprehension.push(#else_key);
4343
}
4444
}
4545
}
@@ -49,9 +49,9 @@ impl quote::ToTokens for BinaryHeapComprehension {
4949
nested_code = quote! {
5050
{
5151
use ::std::collections::BinaryHeap;
52-
let mut __rusthension_binary_heap = BinaryHeap::new();
52+
let mut __binary_heap_comprehension = BinaryHeap::new();
5353
#nested_code
54-
__rusthension_binary_heap
54+
__binary_heap_comprehension
5555
}
5656
};
5757

src/eager_evaluation/hash_map.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ impl quote::ToTokens for HashMapComprehension {
2828

2929
let mut nested_code = match right_expr {
3030
None => quote! {
31-
__rusthension_hash_map.insert(#left_key, #left_value);
31+
__hash_map_comprehension.insert(#left_key, #left_value);
3232
},
3333
Some(MappingElse {
3434
conditions,
@@ -41,9 +41,9 @@ impl quote::ToTokens for HashMapComprehension {
4141

4242
quote! {
4343
if #conditions {
44-
__rusthension_hash_map.insert(#left_key, #left_value);
44+
__hash_map_comprehension.insert(#left_key, #left_value);
4545
} else {
46-
__rusthension_hash_map.insert(#else_key, #else_value);
46+
__hash_map_comprehension.insert(#else_key, #else_value);
4747
}
4848
}
4949
}
@@ -53,9 +53,9 @@ impl quote::ToTokens for HashMapComprehension {
5353
nested_code = quote! {
5454
{
5555
use ::std::collections::HashMap;
56-
let mut __rusthension_hash_map = HashMap::new();
56+
let mut __hash_map_comprehension = HashMap::new();
5757
#nested_code
58-
__rusthension_hash_map
58+
__hash_map_comprehension
5959
}
6060
};
6161

src/eager_evaluation/hash_set.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ impl quote::ToTokens for HashSetComprehension {
2828

2929
let mut nested_code = match right_expr {
3030
None => quote! {
31-
__rusthension_hash_set.insert(#left_key);
31+
__hash_set_comprehension.insert(#left_key);
3232
},
3333
Some(MappingElse {
3434
conditions,
@@ -41,9 +41,9 @@ impl quote::ToTokens for HashSetComprehension {
4141

4242
quote! {
4343
if #conditions {
44-
__rusthension_hash_set.insert(#left_key);
44+
__hash_set_comprehension.insert(#left_key);
4545
} else {
46-
__rusthension_hash_set.insert(#else_key);
46+
__hash_set_comprehension.insert(#else_key);
4747
}
4848
}
4949
}
@@ -53,9 +53,9 @@ impl quote::ToTokens for HashSetComprehension {
5353
nested_code = quote! {
5454
{
5555
use ::std::collections::HashSet;
56-
let mut __rusthension_hash_set = HashSet::new();
56+
let mut __hash_set_comprehension = HashSet::new();
5757
#nested_code
58-
__rusthension_hash_set
58+
__hash_set_comprehension
5959
}
6060
};
6161

src/eager_evaluation/linked_list.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ impl quote::ToTokens for LinkedListComprehension {
2828

2929
let mut nested_code = match right_expr {
3030
None => quote! {
31-
__rusthension_linked_list.push_back(#left_key);
31+
__linked_list_comprehension.push_back(#left_key);
3232
},
3333
Some(MappingElse {
3434
conditions,
@@ -41,9 +41,9 @@ impl quote::ToTokens for LinkedListComprehension {
4141

4242
quote! {
4343
if #conditions {
44-
__rusthension_linked_list.push_back(#left_key);
44+
__linked_list_comprehension.push_back(#left_key);
4545
} else {
46-
__rusthension_linked_list.push_back(#else_key);
46+
__linked_list_comprehension.push_back(#else_key);
4747
}
4848
}
4949
}
@@ -53,9 +53,9 @@ impl quote::ToTokens for LinkedListComprehension {
5353
nested_code = quote! {
5454
{
5555
use ::std::collections::LinkedList;
56-
let mut __rusthension_linked_list = LinkedList::new();
56+
let mut __linked_list_comprehension = LinkedList::new();
5757
#nested_code
58-
__rusthension_linked_list
58+
__linked_list_comprehension
5959
}
6060
};
6161

src/eager_evaluation/vec_deque.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ impl quote::ToTokens for VecDequeComprehension {
2828

2929
let mut nested_code = match right_expr {
3030
None => quote! {
31-
__rusthension_vec_deque.push_back(#left_key);
31+
__vec_deque_comprehension.push_back(#left_key);
3232
},
3333
Some(MappingElse {
3434
conditions,
@@ -41,9 +41,9 @@ impl quote::ToTokens for VecDequeComprehension {
4141

4242
quote! {
4343
if #conditions {
44-
__rusthension_vec_deque.push_back(#left_key);
44+
__vec_deque_comprehension.push_back(#left_key);
4545
} else {
46-
__rusthension_vec_deque.push_back(#else_key);
46+
__vec_deque_comprehension.push_back(#else_key);
4747
}
4848
}
4949
}
@@ -53,9 +53,9 @@ impl quote::ToTokens for VecDequeComprehension {
5353
nested_code = quote! {
5454
{
5555
use ::std::collections::VecDeque;
56-
let mut __rusthension_vec_deque = VecDeque::new();
56+
let mut __vec_deque_comprehension = VecDeque::new();
5757
#nested_code
58-
__rusthension_vec_deque
58+
__vec_deque_comprehension
5959
}
6060
};
6161

src/eager_evaluation/vector.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ impl quote::ToTokens for VecComprehension {
2828

2929
let mut nested_code = match right_expr {
3030
None => quote! {
31-
__rusthension_vec.push(#left_key);
31+
__vector_comprehension.push(#left_key);
3232
},
3333
Some(MappingElse {
3434
conditions,
@@ -41,9 +41,9 @@ impl quote::ToTokens for VecComprehension {
4141

4242
quote! {
4343
if #conditions {
44-
__rusthension_vec.push(#left_key);
44+
__vector_comprehension.push(#left_key);
4545
} else {
46-
__rusthension_vec.push(#else_key);
46+
__vector_comprehension.push(#else_key);
4747
}
4848
}
4949
}
@@ -52,9 +52,9 @@ impl quote::ToTokens for VecComprehension {
5252
nested_code = crate::eager_evaluation::handle_nested_loops(iter_clauses, nested_code);
5353
nested_code = quote! {
5454
{
55-
let mut __rusthension_vec = Vec::new();
55+
let mut __vector_comprehension = Vec::new();
5656
#nested_code
57-
__rusthension_vec
57+
__vector_comprehension
5858
}
5959
};
6060

src/lib.rs

-40
Original file line numberDiff line numberDiff line change
@@ -481,43 +481,3 @@ pub(crate) fn common_parse(
481481

482482
(mapping, iter_clauses)
483483
}
484-
485-
struct IterMethodCallFinder {
486-
is_iter: bool,
487-
}
488-
489-
use syn::{ExprMethodCall, visit::Visit};
490-
impl<'ast> Visit<'ast> for IterMethodCallFinder {
491-
fn visit_expr_method_call(&mut self, node: &'ast ExprMethodCall) {
492-
match *node.receiver {
493-
syn::Expr::Path(_) | syn::Expr::Field(_) => {
494-
if node.method == "iter" {
495-
self.is_iter = true;
496-
}
497-
}
498-
_ => syn::visit::visit_expr(&mut *self, &node.receiver),
499-
}
500-
}
501-
}
502-
503-
pub(crate) fn is_iter(expr: &syn::Expr) -> bool {
504-
let mut finder = IterMethodCallFinder { is_iter: false };
505-
finder.visit_expr(expr);
506-
507-
finder.is_iter
508-
}
509-
510-
#[test]
511-
fn test_is_iter() {
512-
// 最右侧是iter方法
513-
let expr = syn::parse_quote!(some.method_1().method_2().iter());
514-
assert!(!is_iter(&expr));
515-
eprintln!("--------------------------------");
516-
// 最左侧是iter方法
517-
let expr = syn::parse_quote!(some.iter().method_3().method_4());
518-
assert!(is_iter(&expr));
519-
eprintln!("--------------------------------");
520-
521-
let expr = syn::parse_quote!(some.method_5().iter().method_6());
522-
assert!(!is_iter(&expr));
523-
}

0 commit comments

Comments
 (0)