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

all: change optional to result of io (#16075)

This commit is contained in:
yuyi
2022-10-16 14:28:57 +08:00
committed by GitHub
parent 6e46933c55
commit f6844e9766
187 changed files with 1885 additions and 1874 deletions

View File

@@ -107,9 +107,9 @@ pub fn (r BufferedReader) end_of_stream() bool {
// read_line attempts to read a line from the buffered reader
// it will read until it finds a new line character (\n) or
// the end of stream
pub fn (mut r BufferedReader) read_line() ?string {
pub fn (mut r BufferedReader) read_line() !string {
if r.end_of_stream {
return none
return error('none')
}
mut line := []u8{}
for {
@@ -119,7 +119,7 @@ pub fn (mut r BufferedReader) read_line() ?string {
// We are at the end of the stream
if line.len == 0 {
// we had nothing so return nothing
return none
return error('none')
}
return line.bytestr()
}
@@ -144,5 +144,5 @@ pub fn (mut r BufferedReader) read_line() ?string {
line << r.buf[r.offset..i]
r.offset = i
}
return none
return error('none')
}

View File

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

View File

@@ -1,13 +1,13 @@
import io
import os
fn test_cp() ? {
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(mut r, mut stdout)?
io.cp(mut r, mut stdout)!
assert true
}

View File

@@ -21,9 +21,9 @@ fn (mut b Buf) read(mut buf []u8) !int {
return n
}
fn (mut w Writ) write(buf []u8) ?int {
fn (mut w Writ) write(buf []u8) !int {
if buf.len <= 0 {
return none
return error('none')
}
w.bytes << buf
return buf.len

View File

@@ -22,9 +22,9 @@ pub mut:
// 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 (mut m MultiWriter) write(buf []u8) ?int {
pub fn (mut m MultiWriter) write(buf []u8) !int {
for mut w in m.writers {
n := w.write(buf)?
n := w.write(buf)!
if n != buf.len {
return error('io: incomplete write to writer of MultiWriter')
}

View File

@@ -43,7 +43,7 @@ pub mut:
bytes []u8
}
fn (mut w TestWriter) write(buf []u8) ?int {
fn (mut w TestWriter) write(buf []u8) !int {
w.bytes << buf
return buf.len
}
@@ -53,7 +53,7 @@ pub mut:
bytes []u8
}
fn (mut w TestIncompleteWriter) write(buf []u8) ?int {
fn (mut w TestIncompleteWriter) write(buf []u8) !int {
b := buf[..buf.len - 1]
w.bytes << b
return b.len
@@ -61,6 +61,6 @@ fn (mut w TestIncompleteWriter) write(buf []u8) ?int {
struct TestErrorWriter {}
fn (mut w TestErrorWriter) write(buf []u8) ?int {
fn (mut w TestErrorWriter) write(buf []u8) !int {
return error('error writer errored')
}

View File

@@ -44,7 +44,7 @@ mut:
// 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) ?[]u8 {
pub fn read_all(config ReadAllConfig) ![]u8 {
mut r := config.reader
read_till_eof := config.read_to_end_of_stream
@@ -65,11 +65,11 @@ pub fn read_all(config ReadAllConfig) ?[]u8 {
// read_any reads any available bytes from a reader
// (until the reader returns a read of 0 length)
pub fn read_any(mut r Reader) ?[]u8 {
pub fn read_any(mut r Reader) ![]u8 {
mut b := []u8{len: io.read_all_len}
mut read := 0
for {
new_read := r.read(mut b[read..]) or { return none }
new_read := r.read(mut b[read..]) or { return error('none') }
read += new_read
if new_read == 0 {
break
@@ -83,5 +83,5 @@ pub fn read_any(mut r Reader) ?[]u8 {
// RandomReader represents a stream of data that can be read from at a random location
pub interface RandomReader {
read_from(pos u64, mut buf []u8) ?int
read_from(pos u64, mut buf []u8) !int
}

View File

@@ -18,7 +18,7 @@ pub fn (mut r ReaderWriterImpl) read(mut buf []u8) !int {
return r.r.read(mut buf)
}
pub fn (mut r ReaderWriterImpl) write(buf []u8) ?int {
pub fn (mut r ReaderWriterImpl) write(buf []u8) !int {
return r.w.write(buf)
}

View File

@@ -14,7 +14,7 @@ pub struct TempFileOptions {
}
// temp_file returns an uniquely named, open, writable, `os.File` and it's path
pub fn temp_file(tfo TempFileOptions) ?(os.File, string) {
pub fn temp_file(tfo TempFileOptions) !(os.File, string) {
mut d := tfo.path
if d == '' {
d = os.temp_dir()
@@ -47,7 +47,7 @@ pub struct TempDirOptions {
}
// temp_dir returns an uniquely named, writable, directory path
pub fn temp_dir(tdo TempFileOptions) ?string {
pub fn temp_dir(tdo TempFileOptions) !string {
mut d := tdo.path
if d == '' {
d = os.temp_dir()

View File

@@ -3,11 +3,11 @@ module io
// Writer represents a stream of data that can be wrote to
pub interface Writer {
mut:
write(buf []u8) ?int
write(buf []u8) !int
}
// RandomWriter represents a stream of data that can be wrote to
// at a random pos
pub interface RandomWriter {
write_to(pos u64, buf []u8) ?int
write_to(pos u64, buf []u8) !int
}