Skip to content

Commit df887e2

Browse files
committed
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.
1 parent 4735bcc commit df887e2

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

crates/anstream/src/auto.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,17 @@ where
157157
}
158158
}
159159

160+
/// Get the wrapped [`RawStream`]
161+
#[inline]
162+
pub fn as_inner(&self) -> &S {
163+
match &self.inner {
164+
StreamInner::PassThrough(w) => w,
165+
StreamInner::Strip(w) => w.as_inner(),
166+
#[cfg(all(windows, feature = "wincon"))]
167+
StreamInner::Wincon(w) => w.as_inner(),
168+
}
169+
}
170+
160171
/// Returns `true` if the descriptor/handle refers to a terminal/tty.
161172
#[inline]
162173
pub fn is_terminal(&self) -> bool {

crates/anstream/src/strip.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ where
3030
pub fn into_inner(self) -> S {
3131
self.raw
3232
}
33+
34+
/// Get the wrapped [`std::io::Write`]
35+
#[inline]
36+
pub fn as_inner(&self) -> &S {
37+
&self.raw
38+
}
3339
}
3440

3541
impl<S> StripStream<S>

crates/anstream/src/wincon.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ where
3434
pub fn into_inner(self) -> S {
3535
self.raw
3636
}
37+
38+
/// Get the wrapped [`std::io::Write`]
39+
#[inline]
40+
pub fn as_inner(&self) -> &S {
41+
&self.raw
42+
}
3743
}
3844

3945
impl<S> WinconStream<S>

0 commit comments

Comments
 (0)