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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
//!
//! The line buffer stream. Currently under planning.
//!
//! This idea is to make line-by-line processing more efficient.
//!
use crate::*;

use std::io::{BufRead, BufReader, BufWriter, Read, Write};
use std::sync::{Mutex, MutexGuard};

//----------------------------------------------------------------------
//{{{ impl StreamIn
/// The line buffer input stream.
#[derive(Debug)]
pub struct LineIn(LockableLineIn);
impl LineIn {}
impl Default for LineIn {
    fn default() -> Self {
        Self(LockableLineIn::default())
    }
}
impl StreamIn for LineIn {
    fn lock(&self) -> Box<dyn StreamInLock + '_> {
        Box::new(LineInLock(self.0.lock()))
    }
}

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

//----------------------------------------------------------------------
//{{{ impl StreamOut
/// The line buffer output stream.
#[derive(Debug)]
pub struct LineOut(LockableLineOut);
impl LineOut {}
impl StreamOut for LineOut {
    fn lock(&self) -> Box<dyn StreamOutLock + '_> {
        Box::new(LineOutLock(self.0.lock()))
    }
}

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

//----------------------------------------------------------------------
//{{{ impl StreamErr
/// The line buffer error stream.
#[derive(Debug)]
pub struct LineErr(LockableLineErr);
impl LineErr {}
impl StreamErr for LineErr {
    fn lock(&self) -> Box<dyn StreamErrLock + '_> {
        Box::new(LineErrLock(self.0.lock()))
    }
}

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

//----------------------------------------------------------------------
const LINE_BUF_SIZE: usize = 1024;

#[derive(Debug)]
struct LockableLineIn {
    inner: Mutex<BufReader<RawLineIn>>,
}
impl LockableLineIn {
    pub fn new() -> Self {
        LockableLineIn {
            inner: Mutex::new(BufReader::with_capacity(
                LINE_BUF_SIZE,
                RawLineIn::default(),
            )),
        }
    }
    pub fn lock(&self) -> LockableLineInLock<'_> {
        LockableLineInLock {
            inner: self.inner.lock().unwrap_or_else(|e| e.into_inner()),
        }
    }
}
impl Default for LockableLineIn {
    fn default() -> Self {
        Self::new()
    }
}

#[derive(Debug)]
struct LockableLineInLock<'a> {
    inner: MutexGuard<'a, BufReader<RawLineIn>>,
}
impl<'a> Read for LockableLineInLock<'a> {
    fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
        self.inner.read(buf)
    }
}
impl<'a> BufRead for LockableLineInLock<'a> {
    fn fill_buf(&mut self) -> std::io::Result<&[u8]> {
        self.inner.fill_buf()
    }
    fn consume(&mut self, amt: usize) {
        self.inner.consume(amt)
    }
}

#[derive(Debug)]
struct LockableLineOut {
    inner: Mutex<BufWriter<RawLineOut>>,
}
impl LockableLineOut {
    pub fn lock(&self) -> LockableLineOutLock<'_> {
        LockableLineOutLock {
            inner: self.inner.lock().unwrap_or_else(|e| e.into_inner()),
        }
    }
}

#[derive(Debug)]
struct LockableLineOutLock<'a> {
    inner: MutexGuard<'a, BufWriter<RawLineOut>>,
}
impl<'a> Write for LockableLineOutLock<'a> {
    fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
        self.inner.write(buf)
    }
    fn flush(&mut self) -> std::io::Result<()> {
        self.inner.flush()
    }
}

#[derive(Debug)]
struct LockableLineErr {
    inner: Mutex<BufWriter<RawLineErr>>,
}
impl LockableLineErr {
    pub fn lock(&self) -> LockableLineErrLock<'_> {
        LockableLineErrLock {
            inner: self.inner.lock().unwrap_or_else(|e| e.into_inner()),
        }
    }
}

#[derive(Debug)]
struct LockableLineErrLock<'a> {
    inner: MutexGuard<'a, BufWriter<RawLineErr>>,
}
impl<'a> Write for LockableLineErrLock<'a> {
    fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
        self.inner.write(buf)
    }
    fn flush(&mut self) -> std::io::Result<()> {
        self.inner.flush()
    }
}

#[derive(Debug, Default)]
struct RawLineIn {
    buf: Vec<u8>,
}
#[derive(Debug, Default)]
struct RawLineOut {}
#[derive(Debug, Default)]
struct RawLineErr {}

impl Read for RawLineIn {
    fn read(&mut self, _buf: &mut [u8]) -> std::io::Result<usize> {
        Ok(0)
    }
}
impl BufRead for RawLineIn {
    fn fill_buf(&mut self) -> std::io::Result<&[u8]> {
        Ok(&self.buf)
    }
    fn consume(&mut self, _amt: usize) {}
}

impl Write for RawLineOut {
    fn write(&mut self, _buf: &[u8]) -> std::io::Result<usize> {
        Ok(0)
    }
    fn flush(&mut self) -> std::io::Result<()> {
        Ok(())
    }
}
impl Write for RawLineErr {
    fn write(&mut self, _buf: &[u8]) -> std::io::Result<usize> {
        Ok(0)
    }
    fn flush(&mut self) -> std::io::Result<()> {
        Ok(())
    }
}