Skip to content

Commit

Permalink
Add useful methods to RoundInput
Browse files Browse the repository at this point in the history
  • Loading branch information
survived committed Aug 3, 2022
1 parent 3962d22 commit c856347
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions round-based/src/rounds/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,20 @@ impl<M> RoundInput<M> {
expected_msg_type: msg_type,
}
}

/// Construct a new store for broadcast messages
///
/// The same as `RoundInput::new(i, n, MessageType::Broadcast)`
pub fn broadcast(i: u16, n: u16) -> Self {
Self::new(i, n, MessageType::Broadcast)
}

/// Construct a new store for p2p messages
///
/// The same as `RoundInput::new(i, n, MessageType::P2P)`
pub fn p2p(i: u16, n: u16) -> Self {
Self::new(i, n, MessageType::P2P)
}
}

impl<M> MessagesStore for RoundInput<M>
Expand Down Expand Up @@ -300,6 +314,14 @@ impl<M> RoundMsgs<M> {
self.messages
}

/// Iterator over received messages
///
/// Iterator yields `(sender_index, message)`
pub fn enumerated(self) -> impl Iterator<Item = (u16, M)> {
let indexes = (0..self.i).chain(self.i + 1..);
indexes.zip(self.messages)
}

pub fn into_vec_including_me(mut self, my_msg: M) -> Vec<M> {
self.messages.insert(usize::from(self.i), my_msg);
self.messages
Expand Down

0 comments on commit c856347

Please sign in to comment.