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