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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
//! Indexed BGZF reader.
mod builder;
pub use self::builder::Builder;
use std::io::{self, BufRead, Read, Seek, SeekFrom};
use super::{gzi, Reader, VirtualPosition};
/// An indexed BGZF reader.
pub struct IndexedReader<R> {
inner: Reader<R>,
index: gzi::Index,
}
impl<R> IndexedReader<R> {
/// Returns a reference to the underlying reader.
///
/// # Examples
///
/// ```
/// # use std::io;
/// use noodles_bgzf::{self as bgzf, gzi};
/// let reader = bgzf::IndexedReader::new(io::empty(), gzi::Index::default());
/// let inner = reader.get_ref();
/// ```
pub fn get_ref(&self) -> &R {
self.inner.get_ref()
}
/// Returns a mutable reference to the underlying reader.
///
/// # Examples
///
/// ```
/// # use std::io;
/// use noodles_bgzf::{self as bgzf, gzi};
/// let mut reader = bgzf::IndexedReader::new(io::empty(), gzi::Index::default());
/// let inner = reader.get_mut();
/// ```
pub fn get_mut(&mut self) -> &mut R {
self.inner.get_mut()
}
/// Unwraps and returns the underlying writer.
///
/// # Examples
///
/// ```
/// # use std::io;
/// use noodles_bgzf::{self as bgzf, gzi};
/// let reader = bgzf::IndexedReader::new(io::empty(), gzi::Index::default());
/// let inner = reader.into_inner();
/// ```
pub fn into_inner(self) -> R {
self.inner.into_inner()
}
}
impl<R> IndexedReader<R>
where
R: Read,
{
/// Creates an indexed BGZF reader.
///
/// # Examples
///
/// ```
/// # use std::io;
/// use noodles_bgzf::{self as bgzf, gzi};
/// let reader = bgzf::IndexedReader::new(io::empty(), gzi::Index::default());
/// ```
pub fn new(inner: R, index: gzi::Index) -> Self {
Self {
inner: Reader::new(inner),
index,
}
}
/// Returns the current position of the stream.
///
/// # Examples
///
/// ```
/// # use std::io;
/// use noodles_bgzf::{self as bgzf, gzi};
/// let reader = bgzf::IndexedReader::new(io::empty(), gzi::Index::default());
/// assert_eq!(reader.position(), 0);
/// ```
pub fn position(&self) -> u64 {
self.inner.position()
}
/// Returns the current virtual position of the stream.
///
/// # Examples
///
/// ```
/// # use std::io;
/// use noodles_bgzf::{self as bgzf, gzi};
/// let reader = bgzf::IndexedReader::new(io::empty(), gzi::Index::default());
/// assert_eq!(reader.virtual_position(), bgzf::VirtualPosition::from(0));
/// ```
pub fn virtual_position(&self) -> VirtualPosition {
self.inner.virtual_position()
}
/// Returns the associated index.
///
/// # Examples
///
/// ```
/// # use std::io;
/// use noodles_bgzf::{self as bgzf, gzi};
/// let index = gzi::Index::default();
/// let reader = bgzf::IndexedReader::new(io::empty(), index.clone());
/// assert_eq!(reader.index(), &index);
/// ```
pub fn index(&self) -> &gzi::Index {
&self.index
}
}
impl<R> Read for IndexedReader<R>
where
R: Read,
{
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
self.inner.read(buf)
}
}
impl<R> BufRead for IndexedReader<R>
where
R: Read,
{
fn fill_buf(&mut self) -> io::Result<&[u8]> {
self.inner.fill_buf()
}
fn consume(&mut self, amt: usize) {
self.inner.consume(amt);
}
}
impl<R> Seek for IndexedReader<R>
where
R: Read + Seek,
{
fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> {
match pos {
SeekFrom::Start(pos) => self.inner.seek_by_uncompressed_position(&self.index, pos),
_ => unimplemented!(),
}
}
}