noodles_bam/bai/async/io/reader.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 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
mod index;
use tokio::io::{self, AsyncRead};
use self::index::read_index;
use crate::bai::Index;
/// An async BAM index (BAI) reader.
pub struct Reader<R> {
inner: R,
}
impl<R> Reader<R> {
/// Returns a reference to the underlying reader.
///
/// # Examples
///
/// ```
/// use noodles_bam::bai;
/// use tokio::io;
/// let reader = bai::r#async::io::Reader::new(io::empty());
/// let _inner = reader.get_ref();
/// ```
pub fn get_ref(&self) -> &R {
&self.inner
}
/// Returns a mutable reference to the underlying reader.
///
/// # Examples
///
/// ```
/// use noodles_bam::bai;
/// use tokio::io;
/// let mut reader = bai::r#async::io::Reader::new(io::empty());
/// let _inner = reader.get_mut();
/// ```
pub fn get_mut(&mut self) -> &mut R {
&mut self.inner
}
/// Returns the underlying reader.
///
/// # Examples
///
/// ```
/// use noodles_bam::bai;
/// use tokio::io;
/// let reader = bai::r#async::io::Reader::new(io::empty());
/// let _inner = reader.into_inner();
/// ```
pub fn into_inner(self) -> R {
self.inner
}
}
impl<R> Reader<R>
where
R: AsyncRead + Unpin,
{
/// Creates an async BAM index (BAI) reader.
///
/// # Examples
///
/// ```
/// use noodles_bam::bai;
/// use tokio::io;
/// let reader = bai::r#async::io::Reader::new(io::empty());
/// ```
pub fn new(inner: R) -> Self {
Self { inner }
}
/// Reads the BAM index.
///
/// The position of the stream is expected to be at the start.
///
/// # Examples
///
/// ```no_run
/// # #[tokio::main]
/// # async fn main() -> tokio::io::Result<()> {
/// use noodles_bam::bai;
/// use tokio::fs::File;
///
/// let mut reader = File::open("sample.bam.bai")
/// .await
/// .map(bai::r#async::io::Reader::new)?;
///
/// let index = reader.read_index().await?;
/// # Ok(())
/// # }
/// ```
pub async fn read_index(&mut self) -> io::Result<Index> {
read_index(&mut self.inner).await
}
}