1pub mod heap_array;
2pub mod identifiers;
3
4use std::{error::Error, fmt::Debug, ops::Add};
5
6pub use heap_array::{HeapArray, HeapMatrix, RowMajorHeapMatrix};
7pub use identifiers::{FaultyPeer, PeerId, PeerIndex, PeerNumber, ProtocolInfo, SessionId};
8use subtle::Choice;
9use typenum::{NonZero, Unsigned, B1};
10
11pub trait Positive: Unsigned + NonZero + Debug + Eq + Send + Clone {
15 const SIZE: usize;
16}
17impl<T: Unsigned + NonZero + Debug + Eq + Send + Clone> Positive for T {
18 const SIZE: usize = <T as Unsigned>::USIZE;
19}
20
21pub trait NonNegative: Unsigned + Debug + Eq + Send + Clone {}
23impl<T: Unsigned + Debug + Eq + Send + Clone> NonNegative for T {}
24
25pub trait PositivePlusOne: Positive + Add<B1, Output: Positive> {}
28impl<T: Positive + Add<B1, Output: Positive>> PositivePlusOne for T {}
29
30pub trait ConditionallySelectable: Sized {
36 fn conditional_select(a: &Self, b: &Self, choice: Choice) -> Self;
43}
44
45impl<T: subtle::ConditionallySelectable> ConditionallySelectable for T {
46 #[inline]
47 fn conditional_select(a: &Self, b: &Self, choice: Choice) -> Self {
48 <T as subtle::ConditionallySelectable>::conditional_select(a, b, choice)
49 }
50}
51
52pub trait CollectAll<T, E: Error + From<Vec<E>>>: Iterator<Item = Result<T, E>> + Sized {
56 fn collect_all<TC: FromIterator<T> + Extend<T> + Default>(self) -> Result<TC, E> {
60 let (values, errors): (TC, Vec<E>) =
61 itertools::Itertools::partition_map(self, |v| match v {
62 Ok(v) => itertools::Either::Left(v),
63 Err(e) => itertools::Either::Right(e),
64 });
65 match errors.len() {
66 0 => Ok(values),
67 1 => Err(errors.into_iter().next().unwrap()),
68 _ => Err(E::from(errors)),
69 }
70 }
71
72 fn collect_all_vec(self) -> Result<Vec<T>, E> {
76 self.collect_all::<Vec<T>>()
77 }
78
79 fn collect_errors(self) -> Result<(), E> {
81 let errors: Vec<E> = self.filter_map(Result::err).collect();
82 match errors.len() {
83 0 => Ok(()),
84 1 => Err(errors.into_iter().next().unwrap()),
85 _ => Err(E::from(errors)),
86 }
87 }
88}
89
90impl<T, E: Error + From<Vec<E>>, I: Iterator<Item = Result<T, E>>> CollectAll<T, E> for I {}