1 | /// Explicitly lock a [`std::io::Write`]able |
2 | pub trait Lockable { |
3 | type Locked; |
4 | |
5 | /// Get exclusive access to the `AutoStream` |
6 | /// |
7 | /// Why? |
8 | /// - Faster performance when writing in a loop |
9 | /// - Avoid other threads interleaving output with the current thread |
10 | fn lock(self) -> Self::Locked; |
11 | } |
12 | |
13 | impl Lockable for std::io::Stdout { |
14 | type Locked = std::io::StdoutLock<'static>; |
15 | |
16 | #[inline ] |
17 | fn lock(self) -> Self::Locked { |
18 | #[allow (clippy::needless_borrow)] // Its needed to avoid recursion |
19 | (&self).lock() |
20 | } |
21 | } |
22 | |
23 | impl Lockable for std::io::Stderr { |
24 | type Locked = std::io::StderrLock<'static>; |
25 | |
26 | #[inline ] |
27 | fn lock(self) -> Self::Locked { |
28 | #[allow (clippy::needless_borrow)] // Its needed to avoid recursion |
29 | (&self).lock() |
30 | } |
31 | } |
32 | |
33 | #[cfg (all(windows, feature = "wincon" ))] |
34 | impl Lockable for anstyle_wincon::Console<std::io::Stdout> { |
35 | type Locked = anstyle_wincon::Console<std::io::StdoutLock<'static>>; |
36 | |
37 | #[inline ] |
38 | fn lock(self) -> Self::Locked { |
39 | self.lock() |
40 | } |
41 | } |
42 | |
43 | #[cfg (all(windows, feature = "wincon" ))] |
44 | impl Lockable for anstyle_wincon::Console<std::io::Stderr> { |
45 | type Locked = anstyle_wincon::Console<std::io::StderrLock<'static>>; |
46 | |
47 | #[inline ] |
48 | fn lock(self) -> Self::Locked { |
49 | self.lock() |
50 | } |
51 | } |
52 | |