1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

io: mark the mutability requirements of the Writer interface explicitly; swap the io.cp/2 parameter order to be like os.cp/2 (#10091)

This commit is contained in:
Delyan Angelov
2021-05-13 13:06:42 +03:00
committed by GitHub
parent 14b7ce0f04
commit 1086b4ac5e
7 changed files with 24 additions and 10 deletions

View File

@ -4,7 +4,7 @@ const (
buf_max_len = 1024
)
pub fn cp(dst Writer, src Reader) ? {
pub fn cp(src Reader, mut dst Writer) ? {
mut buf := []byte{len: io.buf_max_len}
for {
len := src.read(mut buf) or { break }

13
vlib/io/io_cp_test.v Normal file
View File

@ -0,0 +1,13 @@
import io
import os
fn test_cp() ? {
mut f := os.open(@FILE) or { panic(err) }
defer {
f.close()
}
mut r := io.new_buffered_reader(reader: f)
mut stdout := os.stdout()
io.cp(r, mut stdout) ?
assert true
}

View File

@ -33,9 +33,9 @@ fn test_copy() {
src := Buf{
bytes: 'abcdefghij'.repeat(10).bytes()
}
dst := Writ{
mut dst := Writ{
bytes: []byte{}
}
io.cp(dst, src) or { assert false }
io.cp(src, mut dst) or { assert false }
assert dst.bytes == src.bytes
}

View File

@ -14,7 +14,7 @@ pub fn new_multi_writer(writers ...Writer) Writer {
// MultiWriter writes to all its writers.
pub struct MultiWriter {
pub:
pub mut:
writers []Writer
}
@ -22,8 +22,8 @@ pub:
// written. If any writer fails to write the full length an error is returned
// and writing to other writers stops. If any writer returns an error the error
// is returned immediately and writing to other writers stops.
pub fn (m MultiWriter) write(buf []byte) ?int {
for w in m.writers {
pub fn (mut m MultiWriter) write(buf []byte) ?int {
for mut w in m.writers {
n := w.write(buf) ?
if n != buf.len {
return error('io: incomplete write to writer of MultiWriter')

View File

@ -3,7 +3,7 @@ module io
fn test_multi_writer_write_successful() {
w0 := TestWriter{}
w1 := TestWriter{}
mw := new_multi_writer(w0, w1)
mut mw := new_multi_writer(w0, w1)
n := mw.write('0123456789'.bytes()) or {
assert false
return
@ -16,7 +16,7 @@ fn test_multi_writer_write_successful() {
fn test_multi_writer_write_incomplete() {
w0 := TestWriter{}
w1 := TestIncompleteWriter{}
mw := new_multi_writer(w0, w1)
mut mw := new_multi_writer(w0, w1)
n := mw.write('0123456789'.bytes()) or {
assert w0.bytes == '0123456789'.bytes()
assert w1.bytes == '012345678'.bytes()
@ -29,7 +29,7 @@ fn test_multi_writer_write_error() {
w0 := TestWriter{}
w1 := TestErrorWriter{}
w2 := TestWriter{}
mw := new_multi_writer(w0, w1, w2)
mut mw := new_multi_writer(w0, w1, w2)
n := mw.write('0123456789'.bytes()) or {
assert w0.bytes == '0123456789'.bytes()
assert w2.bytes == []

View File

@ -12,6 +12,7 @@ pub interface ReaderWriter {
// a seperate reader and writer (see fn make_readerwriter)
struct ReaderWriterImpl {
r Reader
mut:
w Writer
}