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