Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 24 additions & 13 deletions programs/settlement/src/settle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,32 @@ struct SettledOrders<'a> {

impl<'a> IntoIterator for SettledOrders<'a> {
type Item = SettledOrder<'a>;
type IntoIter = std::iter::Map<
std::iter::Zip<std::slice::Iter<'a, [AccountView; 2]>, std::slice::Iter<'a, u8>>,
fn((&'a [AccountView; 2], &'a u8)) -> SettledOrder<'a>,
>;
type IntoIter = SettledOrdersIter<'a>;

fn into_iter(self) -> Self::IntoIter {
// A non-capturing closure coerced to a function pointer so the iterator
// type stays nameable in `IntoIter` above.
let pair_to_order: fn((&'a [AccountView; 2], &'a u8)) -> SettledOrder<'a> =
|([order_pda, sell_token_account], &bump)| SettledOrder {
order_pda,
sell_token_account,
bump,
};
self.accounts.iter().zip(self.bumps).map(pair_to_order)
SettledOrdersIter {
accounts: self.accounts.iter(),
bumps: self.bumps.iter(),
}
}
Comment on lines 39 to +44

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For correctness, I'd include an assert here to confirm both collections hold the same amount of elements.

}

struct SettledOrdersIter<'a> {
accounts: std::slice::Iter<'a, [AccountView; 2]>,
bumps: std::slice::Iter<'a, u8>,
}

impl<'a> Iterator for SettledOrdersIter<'a> {
type Item = SettledOrder<'a>;

fn next(&mut self) -> Option<Self::Item> {
let [order_pda, sell_token_account] = self.accounts.next()?;
let &bump = self.bumps.next()?;
Some(SettledOrder {
order_pda,
sell_token_account,
bump,
})
}
}

Expand Down