Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(stream)!: Remove Lockable in favor of methods #128

Merged
merged 1 commit into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 2 additions & 21 deletions crates/anstream/src/auto.rs
Original file line number Diff line number Diff line change
@@ -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"))]
Expand Down Expand Up @@ -178,7 +177,7 @@ impl AutoStream<std::io::Stdout> {
/// - Faster performance when writing in a loop
/// - Avoid other threads interleaving output with the current thread
#[inline]
pub fn lock(self) -> <Self as Lockable>::Locked {
pub fn lock(self) -> AutoStream<std::io::StdoutLock<'static>> {
let inner = match self.inner {
StreamInner::PassThrough(w) => StreamInner::PassThrough(w.lock()),
StreamInner::Strip(w) => StreamInner::Strip(w.lock()),
Expand All @@ -196,7 +195,7 @@ impl AutoStream<std::io::Stderr> {
/// - Faster performance when writing in a loop
/// - Avoid other threads interleaving output with the current thread
#[inline]
pub fn lock(self) -> <Self as Lockable>::Locked {
pub fn lock(self) -> AutoStream<std::io::StderrLock<'static>> {
let inner = match self.inner {
StreamInner::PassThrough(w) => StreamInner::PassThrough(w.lock()),
StreamInner::Strip(w) => StreamInner::Strip(w.lock()),
Expand Down Expand Up @@ -247,21 +246,3 @@ where

// Not bothering with `write_fmt` as it just calls `write_all`
}

impl Lockable for AutoStream<std::io::Stdout> {
type Locked = AutoStream<<std::io::Stdout as Lockable>::Locked>;

#[inline]
fn lock(self) -> Self::Locked {
self.lock()
}
}

impl Lockable for AutoStream<std::io::Stderr> {
type Locked = AutoStream<<std::io::Stderr as Lockable>::Locked>;

#[inline]
fn lock(self) -> Self::Locked {
self.lock()
}
}
2 changes: 0 additions & 2 deletions crates/anstream/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,13 @@ mod buffer;
mod macros;
mod auto;
mod is_terminal;
mod lockable;
mod raw;
mod strip;
#[cfg(all(windows, feature = "wincon"))]
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"))]
Expand Down
31 changes: 0 additions & 31 deletions crates/anstream/src/lockable.rs

This file was deleted.

46 changes: 30 additions & 16 deletions crates/anstream/src/strip.rs
Original file line number Diff line number Diff line change
@@ -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`
Expand Down Expand Up @@ -35,6 +34,36 @@ where
}
}

impl StripStream<std::io::Stdout> {
/// 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<std::io::StdoutLock<'static>> {
StripStream {
raw: self.raw.lock(),
state: self.state,
}
}
}

impl StripStream<std::io::Stderr> {
/// 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<std::io::StderrLock<'static>> {
StripStream {
raw: self.raw.lock(),
state: self.state,
}
}
}

impl<S> IsTerminal for StripStream<S>
where
S: RawStream,
Expand Down Expand Up @@ -116,21 +145,6 @@ fn offset_to(total: &[u8], subslice: &[u8]) -> usize {
subslice as usize - total as usize
}

impl<S> Lockable for StripStream<S>
where
S: Lockable,
{
type Locked = StripStream<<S as Lockable>::Locked>;

#[inline]
fn lock(self) -> Self::Locked {
Self::Locked {
raw: self.raw.lock(),
state: self.state,
}
}
}

#[cfg(test)]
mod test {
use super::*;
Expand Down
55 changes: 30 additions & 25 deletions crates/anstream/src/wincon.rs
Original file line number Diff line number Diff line change
@@ -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`
Expand Down Expand Up @@ -42,6 +41,36 @@ where
}
}

impl WinconStream<std::io::Stdout> {
/// 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<std::io::StdoutLock<'static>> {
WinconStream {
raw: self.raw.lock(),
state: self.state,
}
}
}

impl WinconStream<std::io::Stderr> {
/// 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<std::io::StderrLock<'static>> {
WinconStream {
raw: self.raw.lock(),
state: self.state,
}
}
}

impl<S> IsTerminal for WinconStream<S>
where
S: RawStream,
Expand Down Expand Up @@ -75,30 +104,6 @@ where
}
}

impl Lockable for WinconStream<std::io::Stdout> {
type Locked = WinconStream<std::io::StdoutLock<'static>>;

#[inline]
fn lock(self) -> Self::Locked {
Self::Locked {
raw: self.raw.lock(),
state: self.state,
}
}
}

impl Lockable for WinconStream<std::io::Stderr> {
type Locked = WinconStream<std::io::StderrLock<'static>>;

#[inline]
fn lock(self) -> Self::Locked {
Self::Locked {
raw: self.raw.lock(),
state: self.state,
}
}
}

fn cap_wincon_color(color: anstyle::Color) -> Option<anstyle::AnsiColor> {
match color {
anstyle::Color::Ansi(c) => Some(c),
Expand Down
Loading