v2 / vlib / io / io.v
26 lines · 24 sloc · 720 bytes · 801600c0c7895a8290f23848d15489554f1c564f
Raw
1module io
2
3// CopySettings provides additional options to io.cp
4@[params]
5pub struct CopySettings {
6pub mut:
7 buffer_size int = 64 * 1024 // The buffer size used during the copying. A larger buffer is more performant, but uses more RAM.
8}
9
10// cp copies from `src` to `dst` by allocating
11// a maximum of 1024 bytes buffer for reading
12// until either EOF is reached on `src` or an error occurs.
13// An error is returned if an error is encountered during write.
14@[manualfree]
15pub fn cp(mut src Reader, mut dst Writer, params CopySettings) ! {
16 mut buf := []u8{len: params.buffer_size}
17 defer {
18 unsafe {
19 buf.free()
20 }
21 }
22 for {
23 bytes := src.read(mut buf) or { break }
24 dst.write(buf[..bytes]) or { return err }
25 }
26}
27