noodles_fasta/fai/async/io/
writer.rs

1use tokio::io::{self, AsyncWrite, AsyncWriteExt};
2
3use crate::fai::{Index, Record};
4
5/// An async FASTA index (FAI) writer.
6pub struct Writer<W> {
7    inner: W,
8}
9
10impl<W> Writer<W> {
11    /// Returns a reference to the underlying writer.
12    ///
13    /// # Examples
14    ///
15    /// ```
16    /// use noodles_fasta::fai;
17    /// use tokio::io;
18    /// let writer = fai::r#async::io::Writer::new(io::sink());
19    /// let _inner = writer.get_ref();
20    /// ```
21    pub fn get_ref(&self) -> &W {
22        &self.inner
23    }
24
25    /// Returns a mutable reference to the underlying writer.
26    ///
27    /// # Examples
28    ///
29    /// ```
30    /// use noodles_fasta::fai;
31    /// use tokio::io;
32    /// let mut writer = fai::r#async::io::Writer::new(io::sink());
33    /// let _inner = writer.get_mut();
34    /// ```
35    pub fn get_mut(&mut self) -> &mut W {
36        &mut self.inner
37    }
38
39    /// Returns the underlying writer.
40    ///
41    /// # Examples
42    ///
43    /// ```
44    /// use noodles_fasta::fai;
45    /// use tokio::io;
46    /// let writer = fai::r#async::io::Writer::new(io::sink());
47    /// let _inner = writer.into_inner();
48    /// ```
49    pub fn into_inner(self) -> W {
50        self.inner
51    }
52}
53
54impl<W> Writer<W>
55where
56    W: AsyncWrite + Unpin,
57{
58    /// Creates an async BAM index (BAI) writer.
59    ///
60    /// # Examples
61    ///
62    /// ```
63    /// use noodles_fasta::fai;
64    /// use tokio::io;
65    /// let writer = fai::r#async::io::Writer::new(io::sink());
66    /// ```
67    pub fn new(inner: W) -> Self {
68        Self { inner }
69    }
70
71    /// Shuts down the output stream.
72    ///
73    /// # Examples
74    ///
75    /// ```
76    /// # #[tokio::main]
77    /// # async fn main() -> tokio::io::Result<()> {
78    /// use noodles_fasta::fai;
79    /// use tokio::io;
80    /// let mut writer = fai::r#async::io::Writer::new(io::sink());
81    /// writer.shutdown().await?;
82    /// # Ok(())
83    /// # }
84    /// ```
85    pub async fn shutdown(&mut self) -> io::Result<()> {
86        self.inner.shutdown().await
87    }
88
89    /// Writes a FASTA index.
90    ///
91    /// The position of the stream is expected to be at the start.
92    ///
93    /// # Examples
94    ///
95    /// ```
96    /// # #[tokio::main]
97    /// # async fn main() -> tokio::io::Result<()> {
98    /// use noodles_fasta::fai;
99    /// use tokio::io;
100    ///
101    /// let mut writer = fai::r#async::io::Writer::new(io::sink());
102    ///
103    /// let index = fai::Index::default();
104    /// writer.write_index(&index).await?;
105    /// # Ok(())
106    /// # }
107    /// ```
108    pub async fn write_index(&mut self, index: &Index) -> io::Result<()> {
109        for record in index.as_ref() {
110            write_record(&mut self.inner, record).await?;
111        }
112
113        Ok(())
114    }
115}
116
117async fn write_record<W>(writer: &mut W, record: &Record) -> io::Result<()>
118where
119    W: AsyncWrite + Unpin,
120{
121    use crate::fai::io::writer::write_record;
122
123    let mut buf = Vec::new();
124    write_record(&mut buf, record)?;
125    writer.write_all(&buf).await
126}