From 8b3c1ac116dec4dcbb06d26671d57daa7429c8cc Mon Sep 17 00:00:00 2001 From: Michael Macias Date: Mon, 10 Jun 2024 10:25:19 -0500 Subject: [PATCH] csi/writer: Add common methods to access the underlying writer --- noodles-csi/CHANGELOG.md | 9 +++++- noodles-csi/src/async/writer.rs | 57 +++++++++++++++++++++++++-------- noodles-csi/src/writer.rs | 42 ++++++++++++++++++++++++ 3 files changed, 94 insertions(+), 14 deletions(-) diff --git a/noodles-csi/CHANGELOG.md b/noodles-csi/CHANGELOG.md index 022747f65..df0c13c96 100644 --- a/noodles-csi/CHANGELOG.md +++ b/noodles-csi/CHANGELOG.md @@ -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 diff --git a/noodles-csi/src/async/writer.rs b/noodles-csi/src/async/writer.rs index 70df153f9..6d0899309 100644 --- a/noodles-csi/src/async/writer.rs +++ b/noodles-csi/src/async/writer.rs @@ -18,22 +18,33 @@ pub struct Writer { inner: bgzf::AsyncWriter, } -impl Writer -where - W: AsyncWrite + Unpin, -{ - /// Creates an async CSI writer. +impl Writer { + /// 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 { + &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 { + &mut self.inner } /// Returns the underlying writer. @@ -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 { + self.inner + } +} + +impl Writer +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. diff --git a/noodles-csi/src/writer.rs b/noodles-csi/src/writer.rs index 4574d9318..6c8197867 100644 --- a/noodles-csi/src/writer.rs +++ b/noodles-csi/src/writer.rs @@ -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 { + &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 { + &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 { + self.inner + } + /// Writes a coordinate-sorted index (CSI). /// /// # Examples