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

pull/10099/head
Delyan Angelov 2021-05-13 13:06:42 +03:00 committed by GitHub
parent 14b7ce0f04
commit 1086b4ac5e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 24 additions and 10 deletions

View File

@ -4,7 +4,7 @@ const (
buf_max_len = 1024 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} mut buf := []byte{len: io.buf_max_len}
for { for {
len := src.read(mut buf) or { break } len := src.read(mut buf) or { break }

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{ src := Buf{
bytes: 'abcdefghij'.repeat(10).bytes() bytes: 'abcdefghij'.repeat(10).bytes()
} }
dst := Writ{ mut dst := Writ{
bytes: []byte{} bytes: []byte{}
} }
io.cp(dst, src) or { assert false } io.cp(src, mut dst) or { assert false }
assert dst.bytes == src.bytes 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. // MultiWriter writes to all its writers.
pub struct MultiWriter { pub struct MultiWriter {
pub: pub mut:
writers []Writer writers []Writer
} }
@ -22,8 +22,8 @@ pub:
// written. If any writer fails to write the full length an error is returned // 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 // and writing to other writers stops. If any writer returns an error the error
// is returned immediately and writing to other writers stops. // is returned immediately and writing to other writers stops.
pub fn (m MultiWriter) write(buf []byte) ?int { pub fn (mut m MultiWriter) write(buf []byte) ?int {
for w in m.writers { for mut w in m.writers {
n := w.write(buf) ? n := w.write(buf) ?
if n != buf.len { if n != buf.len {
return error('io: incomplete write to writer of MultiWriter') return error('io: incomplete write to writer of MultiWriter')

View File

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

View File

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

View File

@ -19,7 +19,7 @@ const (
'float', 'for', 'free', 'goto', 'if', 'inline', 'int', 'link', 'long', 'malloc', 'namespace', 'float', 'for', 'free', 'goto', 'if', 'inline', 'int', 'link', 'long', 'malloc', 'namespace',
'new', 'panic', 'register', 'restrict', 'return', 'short', 'signed', 'sizeof', 'static', 'new', 'panic', 'register', 'restrict', 'return', 'short', 'signed', 'sizeof', 'static',
'struct', 'switch', 'typedef', 'typename', 'union', 'unix', 'unsigned', 'void', 'volatile', 'struct', 'switch', 'typedef', 'typename', 'union', 'unix', 'unsigned', 'void', 'volatile',
'while', 'template'] 'while', 'template', 'stdout', 'stdin', 'stderr']
c_reserved_map = string_array_to_map(c_reserved) c_reserved_map = string_array_to_map(c_reserved)
// same order as in token.Kind // same order as in token.Kind
cmp_str = ['eq', 'ne', 'gt', 'lt', 'ge', 'le'] cmp_str = ['eq', 'ne', 'gt', 'lt', 'ge', 'le']