Skip to content

Commit

Permalink
fasta/fai/async: Add writer
Browse files Browse the repository at this point in the history
  • Loading branch information
zaeleus committed Jan 15, 2025
1 parent 242d3be commit 42beb12
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 3 deletions.
2 changes: 2 additions & 0 deletions noodles-fasta/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
* fasta/fai/async/fs: Add convenience read function
(`fai::r#async::fs::read`).

* fasta/fai/async/io: Add writer (`Writer`).

* fasta/fai/fs: Add convenience write function (`fai::fs::write`).

### Changed
Expand Down
3 changes: 2 additions & 1 deletion noodles-fasta/src/fai/async/io.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Async FAI I/O.
mod reader;
mod writer;

pub use self::reader::Reader;
pub use self::{reader::Reader, writer::Writer};
126 changes: 126 additions & 0 deletions noodles-fasta/src/fai/async/io/writer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
use tokio::io::{self, AsyncWrite, AsyncWriteExt};

use crate::fai::{Index, Record};

/// An async FASTA index (FAI) writer.
pub struct Writer<W> {
inner: W,
}

impl<W> Writer<W> {
/// Returns a reference to the underlying writer.
///
/// # Examples
///
/// ```
/// use noodles_fasta::fai;
/// use tokio::io;
/// let writer = fai::r#async::io::Writer::new(io::sink());
/// let _inner = writer.get_ref();
/// ```
pub fn get_ref(&self) -> &W {
&self.inner
}

/// Returns a mutable reference to the underlying writer.
///
/// # Examples
///
/// ```
/// use noodles_fasta::fai;
/// use tokio::io;
/// let mut writer = fai::r#async::io::Writer::new(io::sink());
/// let _inner = writer.get_mut();
/// ```
pub fn get_mut(&mut self) -> &mut W {
&mut self.inner
}

/// Returns the underlying writer.
///
/// # Examples
///
/// ```
/// use noodles_fasta::fai;
/// use tokio::io;
/// let writer = fai::r#async::io::Writer::new(io::sink());
/// let _inner = writer.into_inner();
/// ```
pub fn into_inner(self) -> W {
self.inner
}
}

impl<W> Writer<W>
where
W: AsyncWrite + Unpin,
{
/// Creates an async BAM index (BAI) writer.
///
/// # Examples
///
/// ```
/// use noodles_fasta::fai;
/// use tokio::io;
/// let writer = fai::r#async::io::Writer::new(io::sink());
/// ```
pub fn new(inner: W) -> Self {
Self { inner }
}

/// Shuts down the output stream.
///
/// # Examples
///
/// ```
/// # #[tokio::main]
/// # async fn main() -> tokio::io::Result<()> {
/// use noodles_fasta::fai;
/// use tokio::io;
/// let mut writer = fai::r#async::io::Writer::new(io::sink());
/// writer.shutdown().await?;
/// # Ok(())
/// # }
/// ```
pub async fn shutdown(&mut self) -> io::Result<()> {
self.inner.shutdown().await
}

/// Writes a FASTA index.
///
/// The position of the stream is expected to be at the start.
///
/// # Examples
///
/// ```
/// # #[tokio::main]
/// # async fn main() -> tokio::io::Result<()> {
/// use noodles_fasta::fai;
/// use tokio::io;
///
/// let mut writer = fai::r#async::io::Writer::new(io::sink());
///
/// let index = fai::Index::default();
/// writer.write_index(&index).await?;
/// # Ok(())
/// # }
/// ```
pub async fn write_index(&mut self, index: &Index) -> io::Result<()> {
for record in index.as_ref() {
write_record(&mut self.inner, record).await?;
}

Ok(())
}
}

async fn write_record<W>(writer: &mut W, record: &Record) -> io::Result<()>
where
W: AsyncWrite + Unpin,
{
use crate::fai::io::writer::write_record;

let mut buf = Vec::new();
write_record(&mut buf, record)?;
writer.write_all(&buf).await
}
2 changes: 1 addition & 1 deletion noodles-fasta/src/fai/io.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! FAI I/O.
mod reader;
mod writer;
pub(crate) mod writer;

pub use self::{reader::Reader, writer::Writer};
2 changes: 1 addition & 1 deletion noodles-fasta/src/fai/io/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ where
}
}

fn write_record<W>(writer: &mut W, record: &Record) -> io::Result<()>
pub(crate) fn write_record<W>(writer: &mut W, record: &Record) -> io::Result<()>
where
W: Write,
{
Expand Down

0 comments on commit 42beb12

Please sign in to comment.