noodles_bgzf/gzi/async/io/writer.rs
1mod index;
2
3use tokio::io::{self, AsyncWrite};
4
5use self::index::write_index;
6use crate::gzi::Index;
7
8/// An async GZ index (GZI) writer.
9pub struct Writer<W> {
10 inner: W,
11}
12
13impl<W> Writer<W> {
14 /// Returns a reference to the underlying writer.
15 ///
16 /// # Examples
17 ///
18 /// ```
19 /// use noodles_bgzf::gzi;
20 /// use tokio::io;
21 /// let writer = gzi::r#async::io::Writer::new(io::sink());
22 /// let _inner = writer.get_ref();
23 /// ```
24 pub fn get_ref(&self) -> &W {
25 &self.inner
26 }
27
28 /// Returns a mutable reference to the underlying writer.
29 ///
30 /// # Examples
31 ///
32 /// ```
33 /// use noodles_bgzf::gzi;
34 /// use tokio::io;
35 /// let mut writer = gzi::r#async::io::Writer::new(io::sink());
36 /// let _inner = writer.get_mut();
37 /// ```
38 pub fn get_mut(&mut self) -> &mut W {
39 &mut self.inner
40 }
41
42 /// Returns the underlying writer.
43 ///
44 /// # Examples
45 ///
46 /// ```
47 /// use noodles_bgzf::gzi;
48 /// use tokio::io;
49 /// let writer = gzi::r#async::io::Writer::new(io::sink());
50 /// let _inner = writer.into_inner();
51 /// ```
52 pub fn into_inner(self) -> W {
53 self.inner
54 }
55}
56
57impl<W> Writer<W>
58where
59 W: AsyncWrite + Unpin,
60{
61 /// Creates an async GZ index writer.
62 ///
63 /// # Examples
64 ///
65 /// ```
66 /// use noodles_bgzf::gzi;
67 /// use tokio::io;
68 /// let writer = gzi::r#async::io::Writer::new(io::sink());
69 /// ```
70 pub fn new(inner: W) -> Self {
71 Self { inner }
72 }
73
74 /// Writes a GZ index.
75 ///
76 /// # Examples
77 ///
78 /// ```
79 /// # #[tokio::main]
80 /// # async fn main() -> tokio::io::Result<()> {
81 /// use noodles_bgzf::gzi;
82 /// use tokio::io;
83 /// let index = gzi::Index::default();
84 /// let mut writer = gzi::r#async::io::Writer::new(io::sink());
85 /// writer.write_index(&index).await?;
86 /// # Ok(())
87 /// # }
88 /// ```
89 pub async fn write_index(&mut self, index: &Index) -> io::Result<()> {
90 write_index(&mut self.inner, index).await
91 }
92}