2021-04-25 21:22:33 +03:00
|
|
|
import io
|
|
|
|
|
|
|
|
struct Buf {
|
|
|
|
pub:
|
2022-04-15 15:35:35 +03:00
|
|
|
bytes []u8
|
2021-04-25 21:22:33 +03:00
|
|
|
mut:
|
|
|
|
i int
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Writ {
|
|
|
|
pub mut:
|
2022-04-15 15:35:35 +03:00
|
|
|
bytes []u8
|
2021-04-25 21:22:33 +03:00
|
|
|
}
|
|
|
|
|
2022-08-08 02:33:25 +03:00
|
|
|
fn (mut b Buf) read(mut buf []u8) !int {
|
2021-04-25 21:22:33 +03:00
|
|
|
if !(b.i < b.bytes.len) {
|
2022-08-08 02:33:25 +03:00
|
|
|
return IError(io.Eof{})
|
2021-04-25 21:22:33 +03:00
|
|
|
}
|
2022-03-09 21:26:00 +03:00
|
|
|
n := copy(mut buf, b.bytes[b.i..])
|
2021-04-25 21:22:33 +03:00
|
|
|
b.i += n
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
2022-10-16 09:28:57 +03:00
|
|
|
fn (mut w Writ) write(buf []u8) !int {
|
2021-04-25 21:22:33 +03:00
|
|
|
if buf.len <= 0 {
|
2022-10-16 09:28:57 +03:00
|
|
|
return error('none')
|
2021-04-25 21:22:33 +03:00
|
|
|
}
|
|
|
|
w.bytes << buf
|
|
|
|
return buf.len
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_copy() {
|
2021-10-11 15:41:31 +03:00
|
|
|
mut src := Buf{
|
2021-04-25 21:22:33 +03:00
|
|
|
bytes: 'abcdefghij'.repeat(10).bytes()
|
|
|
|
}
|
2021-05-13 13:06:42 +03:00
|
|
|
mut dst := Writ{
|
2022-04-15 15:35:35 +03:00
|
|
|
bytes: []u8{}
|
2021-04-25 21:22:33 +03:00
|
|
|
}
|
2021-10-11 15:41:31 +03:00
|
|
|
io.cp(mut src, mut dst) or { assert false }
|
2021-04-25 21:22:33 +03:00
|
|
|
assert dst.bytes == src.bytes
|
|
|
|
}
|