noodles_tabix/async/io/
reader.rs

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