2021-04-25 21:22:33 +03:00
|
|
|
import io
|
|
|
|
|
|
|
|
struct Buf {
|
|
|
|
pub:
|
|
|
|
bytes []byte
|
|
|
|
mut:
|
|
|
|
i int
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Writ {
|
|
|
|
pub mut:
|
|
|
|
bytes []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (mut b Buf) read(mut buf []byte) ?int {
|
|
|
|
if !(b.i < b.bytes.len) {
|
|
|
|
return none
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (mut w Writ) write(buf []byte) ?int {
|
|
|
|
if buf.len <= 0 {
|
|
|
|
return none
|
|
|
|
}
|
|
|
|
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{
|
2021-04-25 21:22:33 +03:00
|
|
|
bytes: []byte{}
|
|
|
|
}
|
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
|
|
|
|
}
|