From df887e2a1d946d73ef1f3039998d637e431562a9 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Thu, 2 Oct 2025 11:58:52 -0500 Subject: [PATCH] feat(anstream): Provide read-only access to inner stream The main concern we have is `&mut Inner` because our processing of input is stateful and this can mess things up. Adding an `as_inner` doesn't have that problem and can be useful for any custom behavior users have added that they want to query. This particularly came up with converting Rustup from termcolor to anstream because of their test stream. --- crates/anstream/src/auto.rs | 11 +++++++++++ crates/anstream/src/strip.rs | 6 ++++++ crates/anstream/src/wincon.rs | 6 ++++++ 3 files changed, 23 insertions(+) diff --git a/crates/anstream/src/auto.rs b/crates/anstream/src/auto.rs index 743640f8..16c995d0 100644 --- a/crates/anstream/src/auto.rs +++ b/crates/anstream/src/auto.rs @@ -157,6 +157,17 @@ where } } + /// Get the wrapped [`RawStream`] + #[inline] + pub fn as_inner(&self) -> &S { + match &self.inner { + StreamInner::PassThrough(w) => w, + StreamInner::Strip(w) => w.as_inner(), + #[cfg(all(windows, feature = "wincon"))] + StreamInner::Wincon(w) => w.as_inner(), + } + } + /// Returns `true` if the descriptor/handle refers to a terminal/tty. #[inline] pub fn is_terminal(&self) -> bool { diff --git a/crates/anstream/src/strip.rs b/crates/anstream/src/strip.rs index 9f0d28bd..47a0db0d 100644 --- a/crates/anstream/src/strip.rs +++ b/crates/anstream/src/strip.rs @@ -30,6 +30,12 @@ where pub fn into_inner(self) -> S { self.raw } + + /// Get the wrapped [`std::io::Write`] + #[inline] + pub fn as_inner(&self) -> &S { + &self.raw + } } impl StripStream diff --git a/crates/anstream/src/wincon.rs b/crates/anstream/src/wincon.rs index 570511dd..bb3fbdb8 100644 --- a/crates/anstream/src/wincon.rs +++ b/crates/anstream/src/wincon.rs @@ -34,6 +34,12 @@ where pub fn into_inner(self) -> S { self.raw } + + /// Get the wrapped [`std::io::Write`] + #[inline] + pub fn as_inner(&self) -> &S { + &self.raw + } } impl WinconStream