penumbra_sdk_asset/balance/
iter.rs

1use std::{
2    collections::btree_map,
3    fmt::{self, Debug, Formatter},
4    iter::FusedIterator,
5    num::NonZeroU128,
6};
7
8use crate::{asset::Id, Value};
9
10use super::{Balance, Imbalance};
11
12impl Balance {
13    pub(super) fn iter(&self) -> Iter<'_> {
14        Iter {
15            negated: self.negated,
16            iter: self.balance.iter(),
17        }
18    }
19}
20
21impl IntoIterator for Balance {
22    type Item = Imbalance<Value>;
23    type IntoIter = IntoIter;
24
25    fn into_iter(self) -> Self::IntoIter {
26        IntoIter {
27            negated: self.negated,
28            iter: self.balance.into_iter(),
29        }
30    }
31}
32
33#[derive(Clone)]
34pub struct Iter<'a> {
35    negated: bool,
36    iter: btree_map::Iter<'a, Id, Imbalance<NonZeroU128>>,
37}
38
39impl Debug for Iter<'_> {
40    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
41        f.debug_list().entries(self.clone()).finish()
42    }
43}
44
45impl Iterator for Iter<'_> {
46    type Item = Imbalance<Value>;
47
48    fn next(&mut self) -> Option<Self::Item> {
49        let (&asset_id, &imbalance) = self.iter.next()?;
50        let mut value_imbalance = imbalance.map(move |amount| Value {
51            asset_id,
52            amount: amount.into(),
53        });
54        if self.negated {
55            value_imbalance = -value_imbalance;
56        }
57        Some(value_imbalance)
58    }
59}
60
61impl DoubleEndedIterator for Iter<'_> {
62    fn next_back(&mut self) -> Option<Self::Item> {
63        let (&asset_id, &imbalance) = self.iter.next_back()?;
64        let mut value_imbalance = imbalance.map(move |amount| Value {
65            asset_id,
66            amount: amount.into(),
67        });
68        if self.negated {
69            value_imbalance = -value_imbalance;
70        }
71        Some(value_imbalance)
72    }
73}
74
75impl ExactSizeIterator for Iter<'_> {
76    fn len(&self) -> usize {
77        self.iter.len()
78    }
79}
80
81impl FusedIterator for Iter<'_> {}
82
83pub struct IntoIter {
84    negated: bool,
85    iter: btree_map::IntoIter<Id, Imbalance<NonZeroU128>>,
86}
87
88impl Iterator for IntoIter {
89    type Item = Imbalance<Value>;
90
91    fn next(&mut self) -> Option<Self::Item> {
92        let (asset_id, imbalance) = self.iter.next()?;
93        let mut value_imbalance = imbalance.map(move |amount| Value {
94            asset_id,
95            amount: amount.into(),
96        });
97        if self.negated {
98            value_imbalance = -value_imbalance;
99        }
100        Some(value_imbalance)
101    }
102}
103
104impl DoubleEndedIterator for IntoIter {
105    fn next_back(&mut self) -> Option<Self::Item> {
106        let (asset_id, imbalance) = self.iter.next_back()?;
107        let mut value_imbalance = imbalance.map(move |amount| Value {
108            asset_id,
109            amount: amount.into(),
110        });
111        if self.negated {
112            value_imbalance = -value_imbalance;
113        }
114        Some(value_imbalance)
115    }
116}
117
118impl ExactSizeIterator for IntoIter {
119    fn len(&self) -> usize {
120        self.iter.len()
121    }
122}
123
124impl FusedIterator for IntoIter {}