noodles_bam/io/reader/
record_bufs.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
use std::io::{self, Read};

use noodles_sam::{self as sam, alignment::RecordBuf};

use super::Reader;

/// An iterator over records of a BAM reader.
///
/// This is created by calling [`Reader::record_bufs`].
pub struct RecordBufs<'a, R>
where
    R: Read,
{
    reader: &'a mut Reader<R>,
    header: &'a sam::Header,
    record: RecordBuf,
}

impl<'a, R> RecordBufs<'a, R>
where
    R: Read,
{
    pub(super) fn new(reader: &'a mut Reader<R>, header: &'a sam::Header) -> Self {
        Self {
            reader,
            header,
            record: RecordBuf::default(),
        }
    }
}

impl<'a, R> Iterator for RecordBufs<'a, R>
where
    R: Read,
{
    type Item = io::Result<RecordBuf>;

    fn next(&mut self) -> Option<Self::Item> {
        match self.reader.read_record_buf(self.header, &mut self.record) {
            Ok(0) => None,
            Ok(_) => Some(Ok(self.record.clone())),
            Err(e) => Some(Err(e)),
        }
    }
}