Skip to content

Commit

Permalink
Merge pull request #115 from epage/lock
Browse files Browse the repository at this point in the history
fix(anstream)!: Simplify locking code
  • Loading branch information
epage authored Aug 23, 2023
2 parents e2452e2 + 0c782f6 commit bd2a983
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 41 deletions.
41 changes: 30 additions & 11 deletions crates/anstream/src/auto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,25 @@ where
}
}

impl<S> AutoStream<S>
where
S: Lockable + RawStream,
<S as Lockable>::Locked: RawStream,
{
impl AutoStream<std::io::Stdout> {
/// 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) -> <Self as Lockable>::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<std::io::Stderr> {
/// Get exclusive access to the `AutoStream`
///
/// Why?
Expand Down Expand Up @@ -234,12 +248,17 @@ where
// Not bothering with `write_fmt` as it just calls `write_all`
}

impl<S> Lockable for AutoStream<S>
where
S: Lockable + RawStream,
<S as Lockable>::Locked: RawStream,
{
type Locked = AutoStream<<S as Lockable>::Locked>;
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 {
Expand Down
23 changes: 13 additions & 10 deletions crates/anstream/src/lockable.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
#[cfg(all(windows, feature = "wincon"))]
use crate::RawStream;

/// Explicitly lock a [`std::io::Write`]able
pub trait Lockable {
type Locked;
Expand Down Expand Up @@ -34,15 +31,21 @@ impl Lockable for std::io::Stderr {
}

#[cfg(all(windows, feature = "wincon"))]
impl<S> Lockable for anstyle_wincon::Console<S>
where
S: RawStream + Lockable,
<S as Lockable>::Locked: RawStream,
{
type Locked = anstyle_wincon::Console<<S as Lockable>::Locked>;
impl Lockable for anstyle_wincon::Console<std::io::Stdout> {
type Locked = anstyle_wincon::Console<std::io::StdoutLock<'static>>;

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

#[cfg(all(windows, feature = "wincon"))]
impl Lockable for anstyle_wincon::Console<std::io::Stderr> {
type Locked = anstyle_wincon::Console<std::io::StderrLock<'static>>;

#[inline]
fn lock(self) -> Self::Locked {
self.map(|s| s.lock())
self.lock()
}
}
20 changes: 14 additions & 6 deletions crates/anstream/src/wincon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,20 @@ where
}
}

impl<S> Lockable for WinconStream<S>
where
S: RawStream + Lockable,
<S as Lockable>::Locked: RawStream,
{
type Locked = WinconStream<<S as Lockable>::Locked>;
impl Lockable for WinconStream<std::io::Stdout> {
type Locked = WinconStream<std::io::StdoutLock<'static>>;

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

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

#[inline]
fn lock(self) -> Self::Locked {
Expand Down
14 changes: 0 additions & 14 deletions crates/anstyle-wincon/src/console.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,6 @@ where
self.reset()
}

/// Allow changing the stream
pub fn map<S1: crate::WinconStream + std::io::Write>(
mut self,
op: impl FnOnce(S) -> S1,
) -> Console<S1> {
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 {
Expand Down

0 comments on commit bd2a983

Please sign in to comment.