noodles_tabix/async/io/
writer.rs

1mod index;
2
3use noodles_bgzf as bgzf;
4use tokio::io::{self, AsyncWrite, AsyncWriteExt};
5
6use self::index::write_index;
7use crate::Index;
8
9/// An async tabix writer.
10pub struct Writer<W> {
11    inner: bgzf::AsyncWriter<W>,
12}
13
14impl<W> Writer<W> {
15    /// Returns a reference to the underlying writer.
16    ///
17    /// # Examples
18    ///
19    /// ```
20    /// use noodles_tabix as tabix;
21    /// use tokio::io;
22    /// let writer = tabix::r#async::io::Writer::new(io::sink());
23    /// let _inner = writer.get_ref();
24    /// ```
25    pub fn get_ref(&self) -> &bgzf::AsyncWriter<W> {
26        &self.inner
27    }
28
29    /// Returns a mutable reference to the underlying writer.
30    ///
31    /// # Examples
32    ///
33    /// ```
34    /// use noodles_tabix as tabix;
35    /// use tokio::io;
36    /// let mut writer = tabix::r#async::io::Writer::new(io::sink());
37    /// let _inner = writer.get_mut();
38    /// ```
39    pub fn get_mut(&mut self) -> &mut bgzf::AsyncWriter<W> {
40        &mut self.inner
41    }
42
43    /// Returns the underlying writer.
44    ///
45    /// # Examples
46    ///
47    /// ```
48    /// use noodles_tabix as tabix;
49    /// use tokio::io;
50    /// let writer = tabix::r#async::io::Writer::new(io::sink());
51    /// let _inner = writer.into_inner();
52    /// ```
53    pub fn into_inner(self) -> bgzf::AsyncWriter<W> {
54        self.inner
55    }
56}
57
58impl<W> Writer<W>
59where
60    W: AsyncWrite + Unpin,
61{
62    /// Creates an async tabix writer.
63    ///
64    /// # Examples
65    ///
66    /// ```
67    /// use noodles_tabix as tabix;
68    /// let writer = tabix::r#async::io::Writer::new(Vec::new());
69    /// ```
70    pub fn new(inner: W) -> Self {
71        Self {
72            inner: bgzf::AsyncWriter::new(inner),
73        }
74    }
75
76    /// Shuts down the output stream.
77    ///
78    /// # Examples
79    ///
80    /// ```
81    /// # #[tokio::main]
82    /// # async fn main() -> std::io::Result<()> {
83    /// use noodles_tabix as tabix;
84    /// let mut writer = tabix::r#async::io::Writer::new(Vec::new());
85    /// writer.shutdown().await?;
86    /// # Ok(())
87    /// # }
88    /// ```
89    pub async fn shutdown(&mut self) -> io::Result<()> {
90        self.inner.shutdown().await
91    }
92
93    /// Writes a tabix index.
94    ///
95    /// # Examples
96    ///
97    /// ```
98    /// # #[tokio::main]
99    /// # async fn main() -> std::io::Result<()> {
100    /// use noodles_csi::binning_index::index::Header;
101    /// use noodles_tabix as tabix;
102    ///
103    /// let index = tabix::Index::builder().set_header(Header::default()).build();
104    ///
105    /// let mut writer = tabix::r#async::io::Writer::new(Vec::new());
106    /// writer.write_index(&index).await?;
107    /// # Ok(())
108    /// # }
109    /// ```
110    pub async fn write_index(&mut self, index: &Index) -> io::Result<()> {
111        write_index(&mut self.inner, index).await
112    }
113}