Skip to content

Commit cb08adc

Browse files
committed
Add into_iter_including_me
1 parent ce31b05 commit cb08adc

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

round-based/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "round-based"
3-
version = "0.3.0"
3+
version = "0.3.1"
44
edition = "2021"
55
license = "MIT OR Apache-2.0"
66
description = "Driver for MPC protocols"

round-based/src/rounds_router/simple_store.rs

+58
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,34 @@ impl<M> RoundMsgs<M> {
200200
.chain(self.messages.iter().skip(usize::from(self.i)))
201201
}
202202

203+
/// Returns iterator over received messages plus party's own message
204+
pub fn into_iter_including_me<'m>(self, my_msg: M) -> impl Iterator<Item = M> {
205+
struct InsertsAfter<T, It> {
206+
offset: usize,
207+
inner: It,
208+
item: Option<T>,
209+
}
210+
impl<T, It: Iterator<Item = T>> Iterator for InsertsAfter<T, It> {
211+
type Item = T;
212+
fn next(&mut self) -> Option<Self::Item> {
213+
if self.offset == 0 {
214+
match self.item.take() {
215+
Some(x) => Some(x),
216+
None => self.inner.next(),
217+
}
218+
} else {
219+
self.offset -= 1;
220+
self.inner.next()
221+
}
222+
}
223+
}
224+
InsertsAfter {
225+
offset: usize::from(self.i),
226+
inner: self.messages.into_iter(),
227+
item: Some(my_msg)
228+
}
229+
}
230+
203231
/// Returns iterator over messages with sender indexes
204232
///
205233
/// Iterator yields `(sender_index, msg_id, message)`
@@ -458,4 +486,34 @@ mod tests {
458486
})
459487
.unwrap();
460488
}
489+
490+
#[test]
491+
fn into_iter_including_me() {
492+
let me = -10_isize;
493+
let messages = alloc::vec![1, 2, 3];
494+
495+
let me_first = super::RoundMsgs {
496+
i: 0,
497+
ids: alloc::vec![1, 2, 3],
498+
messages: messages.clone(),
499+
};
500+
let all = me_first.into_iter_including_me(me).collect::<Vec<_>>();
501+
assert_eq!(all, [-10, 1, 2, 3]);
502+
503+
let me_second = super::RoundMsgs {
504+
i: 1,
505+
ids: alloc::vec![0, 2, 3],
506+
messages: messages.clone(),
507+
};
508+
let all = me_second.into_iter_including_me(me).collect::<Vec<_>>();
509+
assert_eq!(all, [1, -10, 2, 3]);
510+
511+
let me_last = super::RoundMsgs {
512+
i: 3,
513+
ids: alloc::vec![0, 1, 2],
514+
messages: messages.clone(),
515+
};
516+
let all = me_last.into_iter_including_me(me).collect::<Vec<_>>();
517+
assert_eq!(all, [1, 2, 3, -10]);
518+
}
461519
}

0 commit comments

Comments
 (0)