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 80d586b
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 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 @@ -304,6 +318,22 @@ impl<M> RoundMsgs<M> {
self.messages.insert(usize::from(self.i), my_msg);
self.messages
}

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

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

#[derive(Debug, Error)]
Expand Down

0 comments on commit 80d586b

Please sign in to comment.