primitives/types/
mod.rs

1pub mod heap_array;
2pub mod identifiers;
3
4use std::{fmt::Debug, ops::Add};
5
6pub use heap_array::{HeapArray, HeapMatrix};
7pub use identifiers::{PeerId, PeerIndex, PeerNumber, SessionId};
8use subtle::Choice;
9use typenum::{NonZero, Unsigned, B1};
10
11// ---------- Typenum aliases ---------- //
12
13/// A trait to represent positive nonzero unsigned integer typenum constants.
14pub trait Positive: Unsigned + NonZero + Debug + Eq + Send {
15    const SIZE: usize;
16}
17impl<T: Unsigned + NonZero + Debug + Eq + Send> 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 {}
23impl<T: Unsigned + Debug + Eq + Send> 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}