pub mod heap_array;
pub mod identifiers;
use std::{error::Error, fmt::Debug, ops::Add};
pub use heap_array::{HeapArray, HeapMatrix, RowMajorHeapMatrix};
pub use identifiers::{FaultyPeer, PeerId, PeerIndex, PeerNumber, ProtocolInfo, SessionId};
use subtle::Choice;
use typenum::{NonZero, Unsigned, B1};
use crate::utils::IntoExactSizeIterator;
pub trait Positive: Unsigned + NonZero + Debug + Eq + Send + Clone {
const SIZE: usize;
}
impl<T: Unsigned + NonZero + Debug + Eq + Send + Clone> Positive for T {
const SIZE: usize = <T as Unsigned>::USIZE;
}
pub trait NonNegative: Unsigned + Debug + Eq + Send + Clone {}
impl<T: Unsigned + Debug + Eq + Send + Clone> NonNegative for T {}
pub trait PositivePlusOne: Positive + Add<B1, Output: Positive> {}
impl<T: Positive + Add<B1, Output: Positive>> PositivePlusOne for T {}
pub trait Batched: Sized + IntoExactSizeIterator<Item = <Self as Batched>::Item> {
type Item;
type Size: Positive;
fn batch_size() -> usize {
Self::Size::SIZE
}
}
pub trait ConditionallySelectable: Sized {
fn conditional_select(a: &Self, b: &Self, choice: Choice) -> Self;
}
impl<T: subtle::ConditionallySelectable> ConditionallySelectable for T {
#[inline]
fn conditional_select(a: &Self, b: &Self, choice: Choice) -> Self {
<T as subtle::ConditionallySelectable>::conditional_select(a, b, choice)
}
}
pub trait CollectAll<T, E: Error + From<Vec<E>>>: Iterator<Item = Result<T, E>> + Sized {
fn collect_all<TC: FromIterator<T> + Extend<T> + Default>(self) -> Result<TC, E> {
let (values, errors): (TC, Vec<E>) =
itertools::Itertools::partition_map(self, |v| match v {
Ok(v) => itertools::Either::Left(v),
Err(e) => itertools::Either::Right(e),
});
match errors.len() {
0 => Ok(values),
1 => Err(errors.into_iter().next().unwrap()),
_ => Err(E::from(errors)),
}
}
fn collect_all_vec(self) -> Result<Vec<T>, E> {
self.collect_all::<Vec<T>>()
}
fn collect_errors(self) -> Result<(), E> {
let errors: Vec<E> = self.filter_map(Result::err).collect();
match errors.len() {
0 => Ok(()),
1 => Err(errors.into_iter().next().unwrap()),
_ => Err(E::from(errors)),
}
}
}
impl<T, E: Error + From<Vec<E>>, I: Iterator<Item = Result<T, E>>> CollectAll<T, E> for I {}