Unbox select traits (#547)

This commit is contained in:
Daniel Sanchez 2024-01-02 12:53:17 +01:00 committed by GitHub
parent 0077c9b6a5
commit a2959712bb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 7 additions and 7 deletions

View File

@ -20,5 +20,5 @@ pub trait BlobCertificateSelect {
fn select_blob_from<'i, I: Iterator<Item = Self::Certificate> + 'i>( fn select_blob_from<'i, I: Iterator<Item = Self::Certificate> + 'i>(
&self, &self,
certificates: I, certificates: I,
) -> Box<dyn Iterator<Item = Self::Certificate> + 'i>; ) -> impl Iterator<Item = Self::Certificate> + 'i;
} }

View File

@ -30,7 +30,7 @@ impl<const SIZE: usize, C: Certificate> BlobCertificateSelect for FillSize<SIZE,
fn select_blob_from<'i, I: Iterator<Item = Self::Certificate> + 'i>( fn select_blob_from<'i, I: Iterator<Item = Self::Certificate> + 'i>(
&self, &self,
certificates: I, certificates: I,
) -> Box<dyn Iterator<Item = Self::Certificate> + 'i> { ) -> impl Iterator<Item = Self::Certificate> + 'i {
utils::select::select_from_till_fill_size::<SIZE, Self::Certificate>( utils::select::select_from_till_fill_size::<SIZE, Self::Certificate>(
|blob| blob.as_bytes().len(), |blob| blob.as_bytes().len(),
certificates, certificates,

View File

@ -28,5 +28,5 @@ pub trait TxSelect {
fn select_tx_from<'i, I: Iterator<Item = Self::Tx> + 'i>( fn select_tx_from<'i, I: Iterator<Item = Self::Tx> + 'i>(
&self, &self,
txs: I, txs: I,
) -> Box<dyn Iterator<Item = Self::Tx> + 'i>; ) -> impl Iterator<Item = Self::Tx> + 'i;
} }

View File

@ -30,7 +30,7 @@ impl<const SIZE: usize, Tx: Transaction> TxSelect for FillSize<SIZE, Tx> {
fn select_tx_from<'i, I: Iterator<Item = Self::Tx> + 'i>( fn select_tx_from<'i, I: Iterator<Item = Self::Tx> + 'i>(
&self, &self,
txs: I, txs: I,
) -> Box<dyn Iterator<Item = Self::Tx> + 'i> { ) -> impl Iterator<Item = Self::Tx> + 'i {
utils::select::select_from_till_fill_size::<SIZE, Self::Tx>(|tx| tx.as_bytes().len(), txs) utils::select::select_from_till_fill_size::<SIZE, Self::Tx>(|tx| tx.as_bytes().len(), txs)
} }
} }

View File

@ -1,10 +1,10 @@
pub fn select_from_till_fill_size<'i, const SIZE: usize, T>( pub fn select_from_till_fill_size<'i, const SIZE: usize, T>(
mut measure: impl FnMut(&T) -> usize + 'i, mut measure: impl FnMut(&T) -> usize + 'i,
items: impl Iterator<Item = T> + 'i, items: impl Iterator<Item = T> + 'i,
) -> Box<dyn Iterator<Item = T> + 'i> { ) -> impl Iterator<Item = T> + 'i {
let mut current_size = 0usize; let mut current_size = 0usize;
Box::new(items.take_while(move |item: &T| { items.take_while(move |item: &T| {
current_size += measure(item); current_size += measure(item);
current_size <= SIZE current_size <= SIZE
})) })
} }