diff --git a/crates/anstream/src/auto.rs b/crates/anstream/src/auto.rs index 44ed0d4b..3fdb3c3a 100644 --- a/crates/anstream/src/auto.rs +++ b/crates/anstream/src/auto.rs @@ -1,7 +1,6 @@ #[cfg(feature = "auto")] use crate::ColorChoice; use crate::IsTerminal; -use crate::Lockable; use crate::RawStream; use crate::StripStream; #[cfg(all(windows, feature = "wincon"))] @@ -178,7 +177,7 @@ impl AutoStream { /// - Faster performance when writing in a loop /// - Avoid other threads interleaving output with the current thread #[inline] - pub fn lock(self) -> ::Locked { + pub fn lock(self) -> AutoStream> { let inner = match self.inner { StreamInner::PassThrough(w) => StreamInner::PassThrough(w.lock()), StreamInner::Strip(w) => StreamInner::Strip(w.lock()), @@ -196,7 +195,7 @@ impl AutoStream { /// - Faster performance when writing in a loop /// - Avoid other threads interleaving output with the current thread #[inline] - pub fn lock(self) -> ::Locked { + pub fn lock(self) -> AutoStream> { let inner = match self.inner { StreamInner::PassThrough(w) => StreamInner::PassThrough(w.lock()), StreamInner::Strip(w) => StreamInner::Strip(w.lock()), @@ -247,21 +246,3 @@ where // Not bothering with `write_fmt` as it just calls `write_all` } - -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 { - self.lock() - } -} diff --git a/crates/anstream/src/lib.rs b/crates/anstream/src/lib.rs index 1799c16b..1a15c31a 100644 --- a/crates/anstream/src/lib.rs +++ b/crates/anstream/src/lib.rs @@ -40,7 +40,6 @@ mod buffer; mod macros; mod auto; mod is_terminal; -mod lockable; mod raw; mod strip; #[cfg(all(windows, feature = "wincon"))] @@ -48,7 +47,6 @@ mod wincon; pub use auto::AutoStream; pub use is_terminal::IsTerminal; -pub use lockable::Lockable; pub use raw::RawStream; pub use strip::StripStream; #[cfg(all(windows, feature = "wincon"))] diff --git a/crates/anstream/src/lockable.rs b/crates/anstream/src/lockable.rs deleted file mode 100644 index 51568f84..00000000 --- a/crates/anstream/src/lockable.rs +++ /dev/null @@ -1,31 +0,0 @@ -/// Explicitly lock a [`std::io::Write`]able -pub trait Lockable { - type Locked; - - /// Get exclusive access to the `AutoStream` - /// - /// Why? - /// - Faster performance when writing in a loop - /// - Avoid other threads interleaving output with the current thread - fn lock(self) -> Self::Locked; -} - -impl Lockable for std::io::Stdout { - type Locked = std::io::StdoutLock<'static>; - - #[inline] - fn lock(self) -> Self::Locked { - #[allow(clippy::needless_borrow)] // Its needed to avoid recursion - (&self).lock() - } -} - -impl Lockable for std::io::Stderr { - type Locked = std::io::StderrLock<'static>; - - #[inline] - fn lock(self) -> Self::Locked { - #[allow(clippy::needless_borrow)] // Its needed to avoid recursion - (&self).lock() - } -} diff --git a/crates/anstream/src/strip.rs b/crates/anstream/src/strip.rs index a5b80987..48bbd340 100644 --- a/crates/anstream/src/strip.rs +++ b/crates/anstream/src/strip.rs @@ -1,6 +1,5 @@ use crate::adapter::StripBytes; use crate::IsTerminal; -use crate::Lockable; use crate::RawStream; /// Only pass printable data to the inner `Write` @@ -35,6 +34,36 @@ where } } +impl StripStream { + /// Get exclusive access to the `StripStream` + /// + /// Why? + /// - Faster performance when writing in a loop + /// - Avoid other threads interleaving output with the current thread + #[inline] + pub fn lock(self) -> StripStream> { + StripStream { + raw: self.raw.lock(), + state: self.state, + } + } +} + +impl StripStream { + /// Get exclusive access to the `StripStream` + /// + /// Why? + /// - Faster performance when writing in a loop + /// - Avoid other threads interleaving output with the current thread + #[inline] + pub fn lock(self) -> StripStream> { + StripStream { + raw: self.raw.lock(), + state: self.state, + } + } +} + impl IsTerminal for StripStream where S: RawStream, @@ -116,21 +145,6 @@ fn offset_to(total: &[u8], subslice: &[u8]) -> usize { subslice as usize - total as usize } -impl Lockable for StripStream -where - S: Lockable, -{ - type Locked = StripStream<::Locked>; - - #[inline] - fn lock(self) -> Self::Locked { - Self::Locked { - raw: self.raw.lock(), - state: self.state, - } - } -} - #[cfg(test)] mod test { use super::*; diff --git a/crates/anstream/src/wincon.rs b/crates/anstream/src/wincon.rs index 44bb512f..3049966b 100644 --- a/crates/anstream/src/wincon.rs +++ b/crates/anstream/src/wincon.rs @@ -1,6 +1,5 @@ use crate::adapter::WinconBytes; use crate::IsTerminal; -use crate::Lockable; use crate::RawStream; /// Only pass printable data to the inner `Write` @@ -42,6 +41,36 @@ where } } +impl WinconStream { + /// Get exclusive access to the `WinconStream` + /// + /// Why? + /// - Faster performance when writing in a loop + /// - Avoid other threads interleaving output with the current thread + #[inline] + pub fn lock(self) -> WinconStream> { + WinconStream { + raw: self.raw.lock(), + state: self.state, + } + } +} + +impl WinconStream { + /// Get exclusive access to the `WinconStream` + /// + /// Why? + /// - Faster performance when writing in a loop + /// - Avoid other threads interleaving output with the current thread + #[inline] + pub fn lock(self) -> WinconStream> { + WinconStream { + raw: self.raw.lock(), + state: self.state, + } + } +} + impl IsTerminal for WinconStream where S: RawStream, @@ -75,30 +104,6 @@ where } } -impl Lockable for WinconStream { - type Locked = WinconStream>; - - #[inline] - fn lock(self) -> Self::Locked { - Self::Locked { - raw: self.raw.lock(), - state: self.state, - } - } -} - -impl Lockable for WinconStream { - type Locked = WinconStream>; - - #[inline] - fn lock(self) -> Self::Locked { - Self::Locked { - raw: self.raw.lock(), - state: self.state, - } - } -} - fn cap_wincon_color(color: anstyle::Color) -> Option { match color { anstyle::Color::Ansi(c) => Some(c),