Struct StreamingIter

Source
pub struct StreamingIter<'r, R> { /* private fields */ }
Expand description

An iterator-like type to “stream” records from a reader.

This API returns records which use the StreamingBody type. This allows reading record headers and metadata without reading the bodies. Bodies can be read or skipped as desired.

This is streaming iterator is particularly useful for streams of records which are indefinite or contain and records of unknown size.

Implementations§

Source§

impl<R: BufRead> StreamingIter<'_, R>

Source

pub fn next_item( &mut self, ) -> Option<Result<Record<StreamingBody<'_, R>>, Error>>

Advance the stream to the next item.

Returns one of the following:

  • Some(Ok(r))` is the next record read from the stream.
  • Some(Err) indicates there was a read error.
  • None indicates no more records are returned.
Examples found in repository?
examples/read_filtered.rs (line 27)
10fn main() -> std::io::Result<()> {
11    let mut args = std::env::args_os().skip(1);
12
13    let warc_name = args
14        .next()
15        .ok_or_else(|| usage_err!("compressed warc filename not supplied"))?;
16
17    let filtered_file_names: Vec<_> = args.map(|s| s.to_string_lossy().to_string()).collect();
18    if filtered_file_names.is_empty() {
19        Err(usage_err!("one or more filtered file names not supplied"))?;
20    }
21
22    let mut file = WarcReader::from_path_gzip(warc_name)?;
23
24    let mut count = 0;
25    let mut skipped = 0;
26    let mut stream_iter = file.stream_records();
27    while let Some(record) = stream_iter.next_item() {
28        let record = record.expect("read of headers ok");
29        count += 1;
30        match record.header(WarcHeader::TargetURI).map(|s| s.to_string()) {
31            Some(v) if has_matching_filename(&v, &filtered_file_names) => {
32                println!("Matches filename, skipping record");
33                skipped += 1;
34            }
35            _ => {
36                let buffered = record.into_buffered().expect("read of record ok");
37                println!(
38                    "Found record. Data:\n{}",
39                    String::from_utf8_lossy(buffered.body())
40                );
41            }
42        }
43    }
44
45    println!("Total records: {}\nSkipped records: {}", count, skipped);
46
47    Ok(())
48}

Auto Trait Implementations§

§

impl<'r, R> Freeze for StreamingIter<'r, R>

§

impl<'r, R> RefUnwindSafe for StreamingIter<'r, R>
where R: RefUnwindSafe,

§

impl<'r, R> Send for StreamingIter<'r, R>
where R: Send,

§

impl<'r, R> Sync for StreamingIter<'r, R>
where R: Sync,

§

impl<'r, R> Unpin for StreamingIter<'r, R>

§

impl<'r, R> !UnwindSafe for StreamingIter<'r, R>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.