2020-11-15 23:54:47 +03:00
|
|
|
module io
|
|
|
|
|
2023-01-25 23:03:20 +03:00
|
|
|
// ReaderWriter represents a stream that can be read and written.
|
2020-11-15 23:54:47 +03:00
|
|
|
pub interface ReaderWriter {
|
2021-10-17 19:39:51 +03:00
|
|
|
Reader
|
|
|
|
Writer
|
2020-11-15 23:54:47 +03:00
|
|
|
}
|
|
|
|
|
2020-12-16 20:22:26 +03:00
|
|
|
// ReaderWriterImpl is a ReaderWriter that can be made from
|
2023-01-25 23:03:20 +03:00
|
|
|
// a separate reader and writer (see fn make_readerwriter).
|
2020-11-15 23:54:47 +03:00
|
|
|
struct ReaderWriterImpl {
|
2021-05-13 13:06:42 +03:00
|
|
|
mut:
|
2021-10-11 15:41:31 +03:00
|
|
|
r Reader
|
2020-11-15 23:54:47 +03:00
|
|
|
w Writer
|
|
|
|
}
|
|
|
|
|
2023-01-25 23:03:20 +03:00
|
|
|
// read reads up to `buf.len` bytes into `buf`. It returns
|
|
|
|
// the number of bytes read or any error encountered.
|
2022-08-08 02:33:25 +03:00
|
|
|
pub fn (mut r ReaderWriterImpl) read(mut buf []u8) !int {
|
2020-11-15 23:54:47 +03:00
|
|
|
return r.r.read(mut buf)
|
|
|
|
}
|
|
|
|
|
2023-01-25 23:03:20 +03:00
|
|
|
// write writes `buf.len` bytes from `buf` to the underlying
|
|
|
|
// data stream. It returns the number of bytes written or any error
|
|
|
|
// encountered.
|
2022-10-16 09:28:57 +03:00
|
|
|
pub fn (mut r ReaderWriterImpl) write(buf []u8) !int {
|
2020-11-15 23:54:47 +03:00
|
|
|
return r.w.write(buf)
|
|
|
|
}
|
|
|
|
|
|
|
|
// make_readerwriter takes a rstream and a wstream and makes
|
2023-01-25 23:03:20 +03:00
|
|
|
// an rwstream with them.
|
2020-11-15 23:54:47 +03:00
|
|
|
pub fn make_readerwriter(r Reader, w Writer) ReaderWriterImpl {
|
2021-07-20 11:17:08 +03:00
|
|
|
return ReaderWriterImpl{
|
2020-12-16 20:22:26 +03:00
|
|
|
r: r
|
|
|
|
w: w
|
|
|
|
}
|
2020-11-15 23:54:47 +03:00
|
|
|
}
|