1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
use crate::{
ir,
InkItemTrait,
InkTraitItem,
InkTraitMessage,
Selector,
};
use std::collections::HashMap;
pub struct IterInkTraitItemsRaw<'a> {
iter: core::slice::Iter<'a, syn::TraitItem>,
}
impl<'a> IterInkTraitItemsRaw<'a> {
pub(super) fn from_raw(item_trait: &'a syn::ItemTrait) -> Self {
Self {
iter: item_trait.items.iter(),
}
}
}
impl<'a> Iterator for IterInkTraitItemsRaw<'a> {
type Item = InkTraitItem<'a>;
fn next(&mut self) -> Option<Self::Item> {
'outer: loop {
match self.iter.next() {
None => return None,
Some(syn::TraitItem::Method(method)) => {
let first_attr = ir::first_ink_attribute(&method.attrs)
.ok()
.flatten()
.expect("unexpected missing ink! attribute for trait method")
.first()
.kind()
.clone();
match first_attr {
ir::AttributeArg::Message => {
return Some(InkTraitItem::Message(InkTraitMessage::new(
method,
)))
}
_ => continue 'outer,
}
}
Some(_) => continue 'outer,
}
}
}
}
pub struct IterInkTraitItems<'a> {
iter: IterInkTraitItemsRaw<'a>,
message_selectors: &'a HashMap<syn::Ident, Selector>,
}
impl<'a> IterInkTraitItems<'a> {
pub(super) fn new(item_trait: &'a InkItemTrait) -> Self {
Self {
iter: IterInkTraitItemsRaw::from_raw(&item_trait.item),
message_selectors: &item_trait.message_selectors,
}
}
}
impl<'a> Iterator for IterInkTraitItems<'a> {
type Item = (InkTraitItem<'a>, Selector);
fn next(&mut self) -> Option<Self::Item> {
self.iter.next().map(|item| {
let selector = self.message_selectors[item.ident()];
(item, selector)
})
}
}