noodles_bam/bai/async/io/reader.rs
1mod index;
2
3use tokio::io::{self, AsyncRead};
4
5use self::index::read_index;
6use crate::bai::Index;
7
8/// An async BAM index (BAI) reader.
9pub struct Reader<R> {
10 inner: R,
11}
12
13impl<R> Reader<R> {
14 /// Returns a reference to the underlying reader.
15 ///
16 /// # Examples
17 ///
18 /// ```
19 /// use noodles_bam::bai;
20 /// use tokio::io;
21 /// let reader = bai::r#async::io::Reader::new(io::empty());
22 /// let _inner = reader.get_ref();
23 /// ```
24 pub fn get_ref(&self) -> &R {
25 &self.inner
26 }
27
28 /// Returns a mutable reference to the underlying reader.
29 ///
30 /// # Examples
31 ///
32 /// ```
33 /// use noodles_bam::bai;
34 /// use tokio::io;
35 /// let mut reader = bai::r#async::io::Reader::new(io::empty());
36 /// let _inner = reader.get_mut();
37 /// ```
38 pub fn get_mut(&mut self) -> &mut R {
39 &mut self.inner
40 }
41
42 /// Returns the underlying reader.
43 ///
44 /// # Examples
45 ///
46 /// ```
47 /// use noodles_bam::bai;
48 /// use tokio::io;
49 /// let reader = bai::r#async::io::Reader::new(io::empty());
50 /// let _inner = reader.into_inner();
51 /// ```
52 pub fn into_inner(self) -> R {
53 self.inner
54 }
55}
56
57impl<R> Reader<R>
58where
59 R: AsyncRead + Unpin,
60{
61 /// Creates an async BAM index (BAI) reader.
62 ///
63 /// # Examples
64 ///
65 /// ```
66 /// use noodles_bam::bai;
67 /// use tokio::io;
68 /// let reader = bai::r#async::io::Reader::new(io::empty());
69 /// ```
70 pub fn new(inner: R) -> Self {
71 Self { inner }
72 }
73
74 /// Reads the BAM index.
75 ///
76 /// The position of the stream is expected to be at the start.
77 ///
78 /// # Examples
79 ///
80 /// ```no_run
81 /// # #[tokio::main]
82 /// # async fn main() -> tokio::io::Result<()> {
83 /// use noodles_bam::bai;
84 /// use tokio::fs::File;
85 ///
86 /// let mut reader = File::open("sample.bam.bai")
87 /// .await
88 /// .map(bai::r#async::io::Reader::new)?;
89 ///
90 /// let index = reader.read_index().await?;
91 /// # Ok(())
92 /// # }
93 /// ```
94 pub async fn read_index(&mut self) -> io::Result<Index> {
95 read_index(&mut self.inner).await
96 }
97}