diff --git a/crates/anstream/src/auto.rs b/crates/anstream/src/auto.rs index a4f71f30..de1b822a 100644 --- a/crates/anstream/src/auto.rs +++ b/crates/anstream/src/auto.rs @@ -171,11 +171,25 @@ where } } -impl AutoStream -where - S: Lockable + RawStream, - ::Locked: RawStream, -{ +impl AutoStream { + /// Get exclusive access to the `AutoStream` + /// + /// Why? + /// - Faster performance when writing in a loop + /// - Avoid other threads interleaving output with the current thread + #[inline] + pub fn lock(self) -> ::Locked { + let inner = match self.inner { + StreamInner::PassThrough(w) => StreamInner::PassThrough(w.lock()), + StreamInner::Strip(w) => StreamInner::Strip(w.lock()), + #[cfg(all(windows, feature = "wincon"))] + StreamInner::Wincon(w) => StreamInner::Wincon(w.lock()), + }; + AutoStream { inner } + } +} + +impl AutoStream { /// Get exclusive access to the `AutoStream` /// /// Why? @@ -234,12 +248,17 @@ where // Not bothering with `write_fmt` as it just calls `write_all` } -impl Lockable for AutoStream -where - S: Lockable + RawStream, - ::Locked: RawStream, -{ - type Locked = AutoStream<::Locked>; +impl Lockable for AutoStream { + type Locked = AutoStream<::Locked>; + + #[inline] + fn lock(self) -> Self::Locked { + self.lock() + } +} + +impl Lockable for AutoStream { + type Locked = AutoStream<::Locked>; #[inline] fn lock(self) -> Self::Locked { diff --git a/crates/anstream/src/lockable.rs b/crates/anstream/src/lockable.rs index fcf20e31..19d06a01 100644 --- a/crates/anstream/src/lockable.rs +++ b/crates/anstream/src/lockable.rs @@ -1,6 +1,3 @@ -#[cfg(all(windows, feature = "wincon"))] -use crate::RawStream; - /// Explicitly lock a [`std::io::Write`]able pub trait Lockable { type Locked; @@ -34,15 +31,21 @@ impl Lockable for std::io::Stderr { } #[cfg(all(windows, feature = "wincon"))] -impl Lockable for anstyle_wincon::Console -where - S: RawStream + Lockable, - ::Locked: RawStream, -{ - type Locked = anstyle_wincon::Console<::Locked>; +impl Lockable for anstyle_wincon::Console { + type Locked = anstyle_wincon::Console>; + + #[inline] + fn lock(self) -> Self::Locked { + self.lock() + } +} + +#[cfg(all(windows, feature = "wincon"))] +impl Lockable for anstyle_wincon::Console { + type Locked = anstyle_wincon::Console>; #[inline] fn lock(self) -> Self::Locked { - self.map(|s| s.lock()) + self.lock() } } diff --git a/crates/anstream/src/wincon.rs b/crates/anstream/src/wincon.rs index 6627bbb5..e29b974d 100644 --- a/crates/anstream/src/wincon.rs +++ b/crates/anstream/src/wincon.rs @@ -78,12 +78,20 @@ where } } -impl Lockable for WinconStream -where - S: RawStream + Lockable, - ::Locked: RawStream, -{ - type Locked = WinconStream<::Locked>; +impl Lockable for WinconStream { + type Locked = WinconStream>; + + #[inline] + fn lock(self) -> Self::Locked { + Self::Locked { + console: self.console.lock(), + state: self.state, + } + } +} + +impl Lockable for WinconStream { + type Locked = WinconStream>; #[inline] fn lock(self) -> Self::Locked { diff --git a/crates/anstyle-wincon/src/console.rs b/crates/anstyle-wincon/src/console.rs index eb7f590e..33584283 100644 --- a/crates/anstyle-wincon/src/console.rs +++ b/crates/anstyle-wincon/src/console.rs @@ -59,20 +59,6 @@ where self.reset() } - /// Allow changing the stream - pub fn map( - mut self, - op: impl FnOnce(S) -> S1, - ) -> Console { - Console { - stream: Some(op(self.stream.take().unwrap())), - initial_fg: self.initial_fg, - initial_bg: self.initial_bg, - last_fg: self.last_fg, - last_bg: self.last_bg, - } - } - /// Get the inner writer #[inline] pub fn into_inner(mut self) -> S {