1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
use crate::{Id, NodeProfile, Nodes}; use std::collections::HashSet; #[derive(Debug, Clone)] pub struct GossipsBuilder { recipient: Id, gossips: HashSet<Id>, } #[derive(Debug, Clone, PartialEq, Eq)] pub struct Gossips(Vec<NodeProfile>); impl GossipsBuilder { pub(crate) fn new(recipient: Id) -> Self { Self { recipient, gossips: HashSet::default(), } } pub fn recipient(&self) -> &Id { &self.recipient } pub fn add(&mut self, node: Id) -> bool { self.gossips.insert(node) } pub(crate) fn build(self, identity: NodeProfile, nodes: &Nodes) -> Gossips { let mut gossips = self .gossips .into_iter() .filter_map(|id| nodes.get(&id)) .map(|node| node.profile().clone()) .collect::<Vec<NodeProfile>>(); gossips.push(identity); Gossips(gossips) } } impl Gossips { pub fn inner(self) -> Vec<NodeProfile> { self.0 } } impl IntoIterator for Gossips { type Item = NodeProfile; type IntoIter = <Vec<Self::Item> as IntoIterator>::IntoIter; fn into_iter(self) -> Self::IntoIter { self.0.into_iter() } } impl From<Vec<NodeProfile>> for Gossips { fn from(gossips: Vec<NodeProfile>) -> Self { Gossips(gossips) } }