Skip to content

Commit

Permalink
csi/writer: Add common methods to access the underlying writer
Browse files Browse the repository at this point in the history
  • Loading branch information
zaeleus committed Jun 10, 2024
1 parent 3a26bdd commit 8b3c1ac
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 14 deletions.
9 changes: 8 additions & 1 deletion noodles-csi/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@

### Added

* csi/reader: Add common methods to access the underlying reader.
* csi: Add common methods to access the underlying I/O.

### Changed

* csi/async/writer: `Writer::into_inner` now returns the inner BGZF writer
instead of `R`.

Use `writer.into_inner().into_inner()` to unwrap into `R`.

## 0.35.0 - 2024-05-16

Expand Down
57 changes: 44 additions & 13 deletions noodles-csi/src/async/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,33 @@ pub struct Writer<W> {
inner: bgzf::AsyncWriter<W>,
}

impl<W> Writer<W>
where
W: AsyncWrite + Unpin,
{
/// Creates an async CSI writer.
impl<W> Writer<W> {
/// Returns a reference to the underlying writer.
///
/// # Examples
///
/// ```
/// use noodles_csi as csi;
/// let writer = csi::AsyncWriter::new(Vec::new());
/// use tokio::io;
/// let writer = csi::r#async::Writer::new(io::sink());
/// let _inner = writer.get_ref();
/// ```
pub fn new(inner: W) -> Self {
Self {
inner: bgzf::AsyncWriter::new(inner),
}
pub fn get_ref(&self) -> &bgzf::AsyncWriter<W> {
&self.inner
}

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

/// Returns the underlying writer.
Expand All @@ -42,11 +53,31 @@ where
///
/// ```
/// use noodles_csi as csi;
/// use tokio::io;
/// let writer = csi::r#async::Writer::new(io::sink());
/// let _inner = writer.into_inner();
/// ```
pub fn into_inner(self) -> bgzf::AsyncWriter<W> {
self.inner
}
}

impl<W> Writer<W>
where
W: AsyncWrite + Unpin,
{
/// Creates an async CSI writer.
///
/// # Examples
///
/// ```
/// use noodles_csi as csi;
/// let writer = csi::AsyncWriter::new(Vec::new());
/// assert!(writer.into_inner().is_empty());
/// ```
pub fn into_inner(self) -> W {
self.inner.into_inner()
pub fn new(inner: W) -> Self {
Self {
inner: bgzf::AsyncWriter::new(inner),
}
}

/// Shuts down the output stream.
Expand Down
42 changes: 42 additions & 0 deletions noodles-csi/src/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,48 @@ where
}
}

/// Returns a reference to the underlying writer.
///
/// # Examples
///
/// ```
/// # use std::io;
/// use noodles_csi as csi;
/// let writer = csi::Writer::new(io::sink());
/// let _inner = writer.get_ref();
/// ```
pub fn get_ref(&self) -> &bgzf::Writer<W> {
&self.inner
}

/// Returns a mutable reference to the underlying writer.
///
/// # Examples
///
/// ```
/// # use std::io;
/// use noodles_csi as csi;
/// let mut writer = csi::Writer::new(io::sink());
/// let _inner = writer.get_mut();
/// ```
pub fn get_mut(&mut self) -> &mut bgzf::Writer<W> {
&mut self.inner
}

/// Returns the underlying writer.
///
/// # Examples
///
/// ```
/// # use std::io;
/// use noodles_csi as csi;
/// let writer = csi::Writer::new(io::sink());
/// let _inner = writer.into_inner();
/// ```
pub fn into_inner(self) -> bgzf::Writer<W> {
self.inner
}

/// Writes a coordinate-sorted index (CSI).
///
/// # Examples
Expand Down

0 comments on commit 8b3c1ac

Please sign in to comment.