checker: add an interface check for mutability, fixes #1081, fixes #7038 (#11963)

This commit is contained in:
Alexander Ivanov 2021-10-11 15:41:31 +03:00 committed by GitHub
parent d0c961ebc0
commit 0386f2bbea
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
40 changed files with 219 additions and 92 deletions

View file

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

View file

@ -8,6 +8,6 @@ fn test_cp() ? {
}
mut r := io.new_buffered_reader(reader: f)
mut stdout := os.stdout()
io.cp(r, mut stdout) ?
io.cp(mut r, mut stdout) ?
assert true
}

View file

@ -30,12 +30,12 @@ fn (mut w Writ) write(buf []byte) ?int {
}
fn test_copy() {
src := Buf{
mut src := Buf{
bytes: 'abcdefghij'.repeat(10).bytes()
}
mut dst := Writ{
bytes: []byte{}
}
io.cp(src, mut dst) or { assert false }
io.cp(mut src, mut dst) or { assert false }
assert dst.bytes == src.bytes
}

View file

@ -6,6 +6,7 @@ pub interface Reader {
// them into buf.
// A type that implements this should return
// `none` on end of stream (EOF) instead of just returning 0
mut:
read(mut buf []byte) ?int
}
@ -17,14 +18,15 @@ const (
// ReadAllConfig allows options to be passed for the behaviour
// of read_all
pub struct ReadAllConfig {
reader Reader
read_to_end_of_stream bool
mut:
reader Reader
}
// read_all reads all bytes from a reader until either a 0 length read
// or if read_to_end_of_stream is true then the end of the stream (`none`)
pub fn read_all(config ReadAllConfig) ?[]byte {
r := config.reader
mut r := config.reader
read_till_eof := config.read_to_end_of_stream
mut b := []byte{len: io.read_all_len}
@ -44,7 +46,7 @@ pub fn read_all(config ReadAllConfig) ?[]byte {
// read_any reads any available bytes from a reader
// (until the reader returns a read of 0 length)
pub fn read_any(r Reader) ?[]byte {
pub fn read_any(mut r Reader) ?[]byte {
mut b := []byte{len: io.read_all_len}
mut read := 0
for {

View file

@ -3,6 +3,7 @@ module io
// ReaderWriter represents a stream that can be read from and wrote to
pub interface ReaderWriter {
// from Reader
mut:
read(mut buf []byte) ?int
// from Writer
write(buf []byte) ?int
@ -11,8 +12,8 @@ pub interface ReaderWriter {
// ReaderWriterImpl is a ReaderWriter that can be made from
// a seperate reader and writer (see fn make_readerwriter)
struct ReaderWriterImpl {
r Reader
mut:
r Reader
w Writer
}

View file

@ -2,6 +2,7 @@ module io
// Writer represents a stream of data that can be wrote to
pub interface Writer {
mut:
write(buf []byte) ?int
}