Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Swapped to Choice everywhere #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ rayon = ["std", "dep:rayon"]

[dependencies]
rayon = { version = "1.7", optional = true }
subtle = "2.6.1"

[dev-dependencies]
rstest = "0.18"
5 changes: 3 additions & 2 deletions src/alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ extern crate alloc;

use alloc::vec::Vec;
use core::slice::Iter as SliceIter;
use subtle::Choice;

use crate::{BitLength, FromBitIterator, GetBit, IntoBitIter, Lsb0, Msb0, ToBits};

Expand Down Expand Up @@ -41,7 +42,7 @@ impl<T> FromBitIterator for Vec<T>
where
T: FromBitIterator,
{
fn from_lsb0_iter(iter: impl IntoIterator<Item = bool>) -> Self {
fn from_lsb0_iter(iter: impl IntoIterator<Item = Choice>) -> Self {
let mut iter = iter.into_iter().peekable();
let mut vec = Vec::new();
while iter.peek().is_some() {
Expand All @@ -50,7 +51,7 @@ where
vec
}

fn from_msb0_iter(iter: impl IntoIterator<Item = bool>) -> Self {
fn from_msb0_iter(iter: impl IntoIterator<Item = Choice>) -> Self {
let mut iter = iter.into_iter().peekable();
let mut vec = Vec::new();
while iter.peek().is_some() {
Expand Down
5 changes: 3 additions & 2 deletions src/array.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
use crate::FromBitIterator;
use subtle::Choice;

impl<const N: usize, T> FromBitIterator for [T; N]
where
T: FromBitIterator,
{
fn from_lsb0_iter(iter: impl IntoIterator<Item = bool>) -> Self {
fn from_lsb0_iter(iter: impl IntoIterator<Item = Choice>) -> Self {
let mut iter = iter.into_iter();
core::array::from_fn(|_| T::from_lsb0_iter(iter.by_ref()))
}

fn from_msb0_iter(iter: impl IntoIterator<Item = bool>) -> Self {
fn from_msb0_iter(iter: impl IntoIterator<Item = Choice>) -> Self {
let mut iter = iter.into_iter();
core::array::from_fn(|_| T::from_msb0_iter(iter.by_ref()))
}
Expand Down
9 changes: 5 additions & 4 deletions src/bool.rs → src/choice.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
use crate::{BitIterable, BitLength, BitOrder, GetBit};
use subtle::Choice;

impl BitLength for bool {
impl BitLength for Choice {
const BITS: usize = 1;
}

impl<O> GetBit<O> for bool
impl<O> GetBit<O> for Choice
where
O: BitOrder,
{
fn get_bit(&self, index: usize) -> bool {
fn get_bit(&self, index: usize) -> Choice {
assert!(index < 1, "index out of bounds");
*self
}
}

impl BitIterable for bool {}
impl BitIterable for Choice {}
81 changes: 63 additions & 18 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@
//!
//! let byte = 0b1010_1010u8;
//!
//! // Convert to a Vec<bool> in Lsb0 order.
//! // Convert to a Vec<Choice> in Lsb0 order.
//! let bits = byte.to_lsb0_vec();
//!
//! assert_eq!(bits, vec![false, true, false, true, false, true, false, true]);
//!
//! // Writing a bit vector using bools is a pain, use a string instead!
//! // Writing a bit vector using Choices is a pain, use a string instead!
//! //
//! // Notice that the string is written in Msb0 order, and we reverse it to Lsb0.
//! let expected_bits = "10101010".iter_bits().rev().collect::<Vec<bool>>();
//! let expected_bits = "10101010".iter_bits().rev().collect::<Vec<Choice>>();
//!
//! assert_eq!(bits, expected_bits);
//!
Expand Down Expand Up @@ -120,7 +120,7 @@
#[cfg(feature = "alloc")]
mod alloc;
mod array;
mod bool;
mod choice;
#[cfg(feature = "rayon")]
mod rayon;
mod slice;
Expand All @@ -141,6 +141,7 @@ pub use self::rayon::{
};

use core::{fmt::Debug, iter::FusedIterator, marker::PhantomData, ops::Range};
use subtle::Choice;

/// Lsb0 bit order.
#[derive(Debug, Clone, Copy)]
Expand Down Expand Up @@ -169,7 +170,7 @@ where
T: GetBit<O> + BitLength,
O: BitOrder,
{
type Item = bool;
type Item = Choice;

fn next(&mut self) -> Option<Self::Item> {
self.range.next().map(|i| self.value.get_bit(i))
Expand Down Expand Up @@ -291,7 +292,7 @@ where
I::Item: GetBit<O> + BitLength,
O: BitOrder,
{
type Item = bool;
type Item = Choice;

fn next(&mut self) -> Option<Self::Item> {
if let Some(item) = &mut self.next {
Expand Down Expand Up @@ -499,8 +500,17 @@ mod tests {
}

#[rstest]
fn test_to_bit_iter_boolvec() {
let bits = vec![false, true, false, true, false, true, false, true];
fn test_to_bit_iter_bitvec() {
let bits = vec![
Choice::from(0),
Choice::from(1),
Choice::from(0),
Choice::from(1),
Choice::from(0),
Choice::from(1),
Choice::from(0),
Choice::from(1),
];

assert_eq!(u8::from_lsb0_iter(bits.iter_lsb0()), 0b10101010);
}
Expand All @@ -518,15 +528,33 @@ mod tests {
for<'a> T: ToBits<'a>,
{
for value in [T::ZERO, T::ONE, T::TWO, T::MAX] {
let expected_msb0_bits = format!("{:0width$b}", value, width = T::BITS).to_bit_vec();
let expected_msb0_bits = format!("{:0width$b}", value, width = T::BITS)
.to_bit_vec()
.into_iter()
.map(|b| b.unwrap_u8())
.collect::<Vec<u8>>();
let expected_lsb0_bits = expected_msb0_bits
.iter()
.copied()
.rev()
.collect::<Vec<bool>>();

assert_eq!(value.to_msb0_vec(), expected_msb0_bits);
assert_eq!(value.to_lsb0_vec(), expected_lsb0_bits);
.collect::<Vec<u8>>();

assert_eq!(
value
.to_msb0_vec()
.iter()
.map(|b| b.unwrap_u8())
.collect::<Vec<u8>>(),
expected_msb0_bits
);
assert_eq!(
value
.to_lsb0_vec()
.iter()
.map(|b| b.unwrap_u8())
.collect::<Vec<u8>>(),
expected_lsb0_bits
);
}
}

Expand All @@ -550,15 +578,32 @@ mod tests {
T::MAX,
width = T::BITS
)
.to_bit_vec();
let expected_lsb0_bits = expected_msb0_bits
.to_bit_vec()
.iter()
.map(|b| b.unwrap_u8())
.collect::<Vec<u8>>();
let expected_lsb0_bits: Vec<u8> = expected_msb0_bits
.chunks(T::BITS)
.flat_map(|chunk| chunk.iter().copied().rev())
.collect::<Vec<bool>>();
.collect();

let slice = [T::ZERO, T::ONE, T::TWO, T::MAX];

assert_eq!(slice.to_msb0_vec(), expected_msb0_bits);
assert_eq!(slice.to_lsb0_vec(), expected_lsb0_bits);
assert_eq!(
slice
.to_msb0_vec()
.iter()
.map(|b| b.unwrap_u8())
.collect::<Vec<u8>>(),
expected_msb0_bits
);
assert_eq!(
slice
.to_lsb0_vec()
.iter()
.map(|b| b.unwrap_u8())
.collect::<Vec<u8>>(),
expected_lsb0_bits
);
}
}
37 changes: 19 additions & 18 deletions src/rayon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use rayon::{
},
prelude::{IndexedParallelIterator, IntoParallelIterator, IntoParallelRefIterator},
};
use subtle::Choice;

use crate::{BitIter, BitIterable, BitLength, BitOrder, GetBit, Lsb0, Msb0};

Expand All @@ -18,9 +19,9 @@ use crate::{BitIter, BitIterable, BitLength, BitOrder, GetBit, Lsb0, Msb0};
/// shared access to the underlying data without cloning.
pub trait ToParallelBits<'a> {
/// The Lsb0 parallel bit iterator type.
type IterLsb0: ParallelIterator<Item = bool>;
type IterLsb0: ParallelIterator<Item = Choice>;
/// The Msb0 parallel bit iterator type.
type IterMsb0: ParallelIterator<Item = bool>;
type IterMsb0: ParallelIterator<Item = Choice>;

/// Creates a parallel bit iterator over `self` in Lsb0 order.
fn par_iter_lsb0(&'a self) -> Self::IterLsb0;
Expand Down Expand Up @@ -56,9 +57,9 @@ where
Self: BitIterable + Send,
{
/// The Lsb0 parallel bit iterator type.
type IterLsb0: ParallelIterator<Item = bool>;
type IterLsb0: ParallelIterator<Item = Choice>;
/// The Msb0 parallel bit iterator type.
type IterMsb0: ParallelIterator<Item = bool>;
type IterMsb0: ParallelIterator<Item = Choice>;

/// Converts `self` into a parallel bit iterator in Lsb0 order.
fn into_par_iter_lsb0(self) -> Self::IterLsb0;
Expand Down Expand Up @@ -97,9 +98,9 @@ where
/// without cloning.
pub trait IntoParallelBitIterator {
/// The Lsb0 parallel bit iterator type.
type IterLsb0: ParallelIterator<Item = bool>;
type IterLsb0: ParallelIterator<Item = Choice>;
/// The Msb0 parallel bit iterator type.
type IterMsb0: ParallelIterator<Item = bool>;
type IterMsb0: ParallelIterator<Item = Choice>;

/// Converts `self` into a parallel bit iterator in Lsb0 order.
fn into_par_iter_lsb0(self) -> Self::IterLsb0;
Expand Down Expand Up @@ -136,9 +137,9 @@ where
/// the underlying data without cloning.
pub trait IntoParallelRefBitIterator<'a> {
/// The Lsb0 parallel bit iterator type.
type IterLsb0: ParallelIterator<Item = bool> + 'a;
type IterLsb0: ParallelIterator<Item = Choice> + 'a;
/// The Msb0 parallel bit iterator type.
type IterMsb0: ParallelIterator<Item = bool> + 'a;
type IterMsb0: ParallelIterator<Item = Choice> + 'a;

/// Creates a parallel bit iterator over `self` in Lsb0 order.
fn par_iter_lsb0(&'a self) -> Self::IterLsb0;
Expand Down Expand Up @@ -197,7 +198,7 @@ where
T: GetBit<O> + BitLength + Clone + Send,
O: BitOrder,
{
type Item = bool;
type Item = Choice;

fn drive_unindexed<C>(self, consumer: C) -> C::Result
where
Expand Down Expand Up @@ -234,7 +235,7 @@ where
T: GetBit<O> + BitLength + Clone + Send,
O: BitOrder,
{
type Item = bool;
type Item = Choice;
type IntoIter = BitIter<T, O>;

fn into_iter(self) -> Self::IntoIter {
Expand Down Expand Up @@ -297,7 +298,7 @@ where
T: GetBit<O> + BitLength + Sync,
O: BitOrder,
{
type Item = bool;
type Item = Choice;

fn drive_unindexed<C>(self, consumer: C) -> C::Result
where
Expand Down Expand Up @@ -334,7 +335,7 @@ where
T: GetBit<O> + BitLength + Sync,
O: BitOrder,
{
type Item = bool;
type Item = Choice;
type IntoIter = BitIter<&'a T, O>;

fn into_iter(self) -> Self::IntoIter {
Expand Down Expand Up @@ -413,14 +414,14 @@ mod tests {
.iter()
.copied()
.rev()
.collect::<Vec<bool>>();
.collect::<Vec<Choice>>();

assert_eq!(
value.into_par_iter_msb0().collect::<Vec<bool>>(),
value.into_par_iter_msb0().collect::<Vec<Choice>>(),
expected_msb0_bits
);
assert_eq!(
value.into_par_iter_lsb0().collect::<Vec<bool>>(),
value.into_par_iter_lsb0().collect::<Vec<Choice>>(),
expected_lsb0_bits
);
}
Expand Down Expand Up @@ -449,16 +450,16 @@ mod tests {
let expected_lsb0_bits = expected_msb0_bits
.chunks(T::BITS)
.flat_map(|chunk| chunk.iter().copied().rev())
.collect::<Vec<bool>>();
.collect::<Vec<Choice>>();

let slice = [T::ZERO, T::ONE, T::TWO, T::MAX];

assert_eq!(
slice.par_iter_msb0().collect::<Vec<bool>>(),
slice.par_iter_msb0().collect::<Vec<Choice>>(),
expected_msb0_bits
);
assert_eq!(
slice.par_iter_lsb0().collect::<Vec<bool>>(),
slice.par_iter_lsb0().collect::<Vec<Choice>>(),
expected_lsb0_bits
);
}
Expand Down
Loading