arcium-primitives 0.4.0

Arcium primitives
Documentation
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;

// ---------- Typenum ---------- //

/// A trait to represent positive nonzero unsigned integer typenum constants.
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;
}

/// A trait to represent nonnegative (zero or positive) unsigned integer typenum constants.
pub trait NonNegative: Unsigned + Debug + Eq + Send + Clone {}
impl<T: Unsigned + Debug + Eq + Send + Clone> NonNegative for T {}

/// A trait to represent positive nonzero unsigned integer typenum constants which accept "+1"
/// operation.
pub trait PositivePlusOne: Positive + Add<B1, Output: Positive> {}
impl<T: Positive + Add<B1, Output: Positive>> PositivePlusOne for T {}

// ---------- Traits ---------- //

/// A trait to define batching types that contain a compile-time known size of elements.
pub trait Batched: Sized + IntoExactSizeIterator<Item = <Self as Batched>::Item> {
    /// The type of elements contained in the batch.
    type Item;

    /// The size of the batch.
    type Size: Positive;

    /// Returns the size of the batch as a usize.
    fn batch_size() -> usize {
        Self::Size::SIZE
    }
}

/// A type which can be conditionally selected with a loose promise of constant time.
/// Compared to `subtle::ConditionallySelectable`, this trait does not require
/// Copy, but the internal elements should be Copy for the promise to loosely hold.
pub trait ConditionallySelectable: Sized {
    /// Select `a` or `b` according to `choice`.
    ///
    /// # Returns
    ///
    /// * `a` if `choice == Choice(0)`;
    /// * `b` if `choice == Choice(1)`.
    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)
    }
}

/// Collect an iterator of Results<T, E> into a container of T and a vector of E,
/// then pack all the errors in a single error of the same type. Requires that E can be
/// constructed from a Vec<E>.
pub trait CollectAll<T, E: Error + From<Vec<E>>>: Iterator<Item = Result<T, E>> + Sized {
    /// Collect the results into a container of successes, or pack all errors.
    /// Contrary to `collect<Result<Vec<T>, E>>`, this method collects all errors
    /// instead of stopping at the first one.
    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)),
        }
    }

    /// Collect the results into a container of successes, or pack all errors.
    /// Contrary to `collect<Result<Vec<T>, E>>`, this method collects all errors
    /// instead of stopping at the first one.
    fn collect_all_vec(self) -> Result<Vec<T>, E> {
        self.collect_all::<Vec<T>>()
    }

    /// Collect the results, returning Ok(()) if all succeeded, or packing all errors
    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 {}