1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
//!
//! The stdio stream. This is thin-wrap of [`std::io::stdin()`],
//! [`std::io::stdout()`], [`std::io::stderr()`].
//!
use crate::*;

use std::io::{BufRead, Read, Write};

//----------------------------------------------------------------------
//{{{ impl StreamIn
/// The standard input stream.
#[derive(Debug)]
pub struct StdIn(std::io::Stdin);
impl StdIn {
    pub fn with(a: std::io::Stdin) -> Self {
        Self(a)
    }
}
impl Default for StdIn {
    fn default() -> Self {
        Self::with(std::io::stdin())
    }
}
impl StreamIn for StdIn {
    fn lock(&self) -> Box<dyn StreamInLock + '_> {
        Box::new(StdInLock(self.0.lock()))
    }
}
impl Read for StdIn {
    fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
        self.lock().read(buf)
    }
}

/// A locked reference to `StdIn`
pub struct StdInLock<'a>(std::io::StdinLock<'a>);
impl<'a> StreamInLock for StdInLock<'a> {}
impl<'a> Read for StdInLock<'a> {
    #[inline(always)]
    fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
        self.0.read(buf)
    }
}
impl<'a> BufRead for StdInLock<'a> {
    #[inline(always)]
    fn fill_buf(&mut self) -> std::io::Result<&[u8]> {
        self.0.fill_buf()
    }
    #[inline(always)]
    fn consume(&mut self, amt: usize) {
        self.0.consume(amt)
    }
}
//}}}

//----------------------------------------------------------------------
//{{{ impl StreamOut
/// The standard output stream.
#[derive(Debug)]
pub struct StdOut(std::io::Stdout);
impl StdOut {
    pub fn with(a: std::io::Stdout) -> Self {
        Self(a)
    }
}
impl Default for StdOut {
    fn default() -> Self {
        Self::with(std::io::stdout())
    }
}
impl StreamOut for StdOut {
    fn lock(&self) -> Box<dyn StreamOutLock + '_> {
        Box::new(StdOutLock(self.0.lock()))
    }
}
impl Write for StdOut {
    fn write(&mut self, buf: &[u8]) -> Result<usize> {
        self.lock().write(buf)
    }
    fn flush(&mut self) -> Result<()> {
        self.lock().flush()
    }
}

/// A locked reference to `StdOut`
pub struct StdOutLock<'a>(std::io::StdoutLock<'a>);
impl<'a> StreamOutLock for StdOutLock<'a> {
    #[inline(always)]
    fn buffer(&self) -> &[u8] {
        b""
    }
    #[inline(always)]
    fn buffer_str(&mut self) -> &str {
        ""
    }
}
impl<'a> Write for StdOutLock<'a> {
    #[inline(always)]
    fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
        self.0.write(buf)
    }
    #[inline(always)]
    fn flush(&mut self) -> std::io::Result<()> {
        self.0.flush()
    }
}
//}}}

//----------------------------------------------------------------------
//{{{ impl StreamErr
/// The standard error stream.
#[derive(Debug)]
pub struct StdErr(std::io::Stderr);
impl StdErr {
    pub fn with(a: std::io::Stderr) -> Self {
        Self(a)
    }
}
impl Default for StdErr {
    fn default() -> Self {
        Self::with(std::io::stderr())
    }
}
impl StreamErr for StdErr {
    #[inline(always)]
    fn lock(&self) -> Box<dyn StreamErrLock + '_> {
        Box::new(StdErrLock(self.0.lock()))
    }
}
impl Write for StdErr {
    fn write(&mut self, buf: &[u8]) -> Result<usize> {
        self.lock().write(buf)
    }
    fn flush(&mut self) -> Result<()> {
        self.lock().flush()
    }
}

/// A locked reference to `StdErr`
pub struct StdErrLock<'a>(std::io::StderrLock<'a>);
impl<'a> StreamErrLock for StdErrLock<'a> {
    #[inline(always)]
    fn buffer(&self) -> &[u8] {
        b""
    }
    #[inline(always)]
    fn buffer_str(&mut self) -> &str {
        ""
    }
}
impl<'a> Write for StdErrLock<'a> {
    #[inline(always)]
    fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
        self.0.write(buf)
    }
    #[inline(always)]
    fn flush(&mut self) -> std::io::Result<()> {
        self.0.flush()
    }
}
//}}}