Skip to main content

primitives/types/
mod.rs

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
11// ---------- Typenum ---------- //
12
13/// A trait to represent positive nonzero unsigned integer typenum constants.
14pub 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
21/// A trait to represent nonnegative (zero or positive) unsigned integer typenum constants.
22pub trait NonNegative: Unsigned + Debug + Eq + Send + Clone {}
23impl<T: Unsigned + Debug + Eq + Send + Clone> NonNegative for T {}
24
25/// A trait to represent positive nonzero unsigned integer typenum constants which accept "+1"
26/// operation.
27pub trait PositivePlusOne: Positive + Add<B1, Output: Positive> {}
28impl<T: Positive + Add<B1, Output: Positive>> PositivePlusOne for T {}
29
30// ---------- Traits ---------- //
31
32/// A type which can be conditionally selected with a loose promise of constant time.
33/// Compared to `subtle::ConditionallySelectable`, this trait does not require
34/// Copy, but the internal elements should be Copy for the promise to loosely hold.
35pub trait ConditionallySelectable: Sized {
36    /// Select `a` or `b` according to `choice`.
37    ///
38    /// # Returns
39    ///
40    /// * `a` if `choice == Choice(0)`;
41    /// * `b` if `choice == Choice(1)`.
42    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
52/// Collect an iterator of Results<T, E> into a container of T and a vector of E,
53/// then pack all the errors in a single error of the same type. Requires that E can be
54/// constructed from a Vec<E>.
55pub trait CollectAll<T, E: Error + From<Vec<E>>>: Iterator<Item = Result<T, E>> + Sized {
56    /// Collect the results into a container of successes, or pack all errors.
57    /// Contrary to `collect<Result<Vec<T>, E>>`, this method collects all errors
58    /// instead of stopping at the first one.
59    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    /// Collect the results into a container of successes, or pack all errors.
73    /// Contrary to `collect<Result<Vec<T>, E>>`, this method collects all errors
74    /// instead of stopping at the first one.
75    fn collect_all_vec(self) -> Result<Vec<T>, E> {
76        self.collect_all::<Vec<T>>()
77    }
78
79    /// Collect the results, returning Ok(()) if all succeeded, or packing all errors
80    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 {}