Skip to content

Commit

Permalink
Merge pull request #1371 from andyleiserson/repeat-n
Browse files Browse the repository at this point in the history
Use repeat_n from std now that it is stable
  • Loading branch information
akoshelev authored Oct 25, 2024
2 parents 47c3350 + ad8ea60 commit 482c15c
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 42 deletions.
2 changes: 1 addition & 1 deletion ipa-core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "ipa-core"
version = "0.1.0"
rust-version = "1.80.0"
rust-version = "1.82.0"
edition = "2021"
build = "build.rs"

Expand Down
30 changes: 0 additions & 30 deletions ipa-core/src/helpers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -634,36 +634,6 @@ where
}
}

pub struct RepeatN<T> {
element: T,
count: usize,
}

// As of Apr. 2024, this is unstable in `std::iter`. It is also available in `itertools`.
// The advantage over `repeat(element).take(count)` that we care about is that this
// implements `ExactSizeIterator`. The other advantage is that `repeat_n` can return
// the original value (saving a clone) on the last iteration.
pub fn repeat_n<T: Clone>(element: T, count: usize) -> RepeatN<T> {
RepeatN { element, count }
}

impl<T: Clone> Iterator for RepeatN<T> {
type Item = T;

fn next(&mut self) -> Option<Self::Item> {
(self.count > 0).then(|| {
self.count -= 1;
self.element.clone()
})
}

fn size_hint(&self) -> (usize, Option<usize>) {
(self.count, Some(self.count))
}
}

impl<T: Clone> ExactSizeIterator for RepeatN<T> {}

#[cfg(all(test, unit_test))]
mod tests {
use super::*;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use std::iter::repeat;
use std::iter::{repeat, repeat_n};

use ipa_step::StepNarrow;

use crate::{
error::Error,
ff::boolean::Boolean,
helpers::repeat_n,
protocol::{
basics::{BooleanProtocols, SecureMul},
boolean::{or::bool_or, NBitStep},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ where

#[cfg(all(test, unit_test))]
mod tests {
use std::iter::{self, repeat_with};
use std::iter::{self, repeat_n, repeat_with};

use curve25519_dalek::Scalar;
use futures::stream::TryStreamExt;
Expand All @@ -378,7 +378,7 @@ mod tests {
use super::*;
use crate::{
ff::{boolean_array::BA64, Serializable},
helpers::{repeat_n, stream::process_slice_by_chunks},
helpers::stream::process_slice_by_chunks,
protocol::{
context::{dzkp_validator::DZKPValidator, UpgradableContext, TEST_DZKP_STEPS},
ipa_prf::{CONV_CHUNK, CONV_PROOF_CHUNK, PRF_CHUNK},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
use std::{convert::Infallible, iter::zip};
use std::{
convert::Infallible,
iter::{repeat_n, zip},
};

use futures::stream;
use futures_util::{future::try_join, stream::unfold, Stream, StreamExt};

use crate::{
error::{Error, LengthError, UnwrapInfallible},
ff::{boolean::Boolean, boolean_array::BooleanArray, Field, U128Conversions},
helpers::{repeat_n, stream::TryFlattenItersExt, TotalRecords},
helpers::{stream::TryFlattenItersExt, TotalRecords},
protocol::{
basics::{SecureMul, ShareKnownValue},
boolean::{and::bool_and_8_bit, or::or},
Expand Down
8 changes: 3 additions & 5 deletions ipa-core/src/protocol/ipa_prf/prf_sharding/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::{
convert::Infallible,
iter,
iter::zip,
iter::{self, repeat_n, zip},
num::NonZeroU32,
ops::{Not, Range},
};
Expand All @@ -20,7 +19,7 @@ use crate::{
boolean_array::{BooleanArray, BA32, BA7},
ArrayAccess, Field, U128Conversions,
},
helpers::{repeat_n, stream::TryFlattenItersExt, TotalRecords},
helpers::{stream::TryFlattenItersExt, TotalRecords},
protocol::{
basics::{select, BooleanArrayMul, BooleanProtocols, Reveal, SecureMul, ShareKnownValue},
boolean::{
Expand Down Expand Up @@ -877,7 +876,7 @@ where

#[cfg(all(test, unit_test))]
pub mod tests {
use std::num::NonZeroU32;
use std::{iter::repeat_n, num::NonZeroU32};

use super::{AttributionOutputs, PrfShardedIpaInputRow};
use crate::{
Expand All @@ -886,7 +885,6 @@ pub mod tests {
boolean_array::{BooleanArray, BA16, BA20, BA3, BA5, BA8},
Field, U128Conversions,
},
helpers::repeat_n,
protocol::ipa_prf::{
oprf_padding::PaddingParameters, prf_sharding::attribute_cap_aggregate,
},
Expand Down

0 comments on commit 482c15c

Please sign in to comment.