This repository has been archived by the owner on Feb 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 224
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ded5266
commit 964ca91
Showing
5 changed files
with
164 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
pub use streaming_iterator::StreamingIterator; | ||
|
||
/// A [`StreamingIterator`] with an internal buffer of [`Vec<u8>`] used to efficiently | ||
/// present items of type `T` as `&[u8]`. | ||
/// It is generic over the type `T` and the transformation `F: T -> &[u8]`. | ||
pub struct BufStreamingIterator<I, F, T> | ||
where | ||
I: Iterator<Item = T>, | ||
F: Fn(T, &mut Vec<u8>), | ||
{ | ||
iterator: I, | ||
f: F, | ||
buffer: Vec<u8>, | ||
is_valid: bool, | ||
} | ||
|
||
impl<I, F, T> BufStreamingIterator<I, F, T> | ||
where | ||
I: Iterator<Item = T>, | ||
F: Fn(T, &mut Vec<u8>), | ||
{ | ||
#[inline] | ||
pub fn new(iterator: I, f: F, buffer: Vec<u8>) -> Self { | ||
Self { | ||
iterator, | ||
f, | ||
buffer, | ||
is_valid: false, | ||
} | ||
} | ||
} | ||
|
||
impl<I, F, T> StreamingIterator for BufStreamingIterator<I, F, T> | ||
where | ||
I: Iterator<Item = T>, | ||
F: Fn(T, &mut Vec<u8>), | ||
{ | ||
type Item = [u8]; | ||
|
||
#[inline] | ||
fn advance(&mut self) { | ||
let a = self.iterator.next(); | ||
if let Some(a) = a { | ||
self.is_valid = true; | ||
self.buffer.clear(); | ||
(self.f)(a, &mut self.buffer); | ||
} else { | ||
self.is_valid = false; | ||
} | ||
} | ||
|
||
#[inline] | ||
fn get(&self) -> Option<&Self::Item> { | ||
if self.is_valid { | ||
Some(&self.buffer) | ||
} else { | ||
None | ||
} | ||
} | ||
|
||
#[inline] | ||
fn size_hint(&self) -> (usize, Option<usize>) { | ||
self.iterator.size_hint() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters