mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
io: migrate the Reader API to Result instead of Option (#15229)
This commit is contained in:
@@ -36,20 +36,23 @@ pub fn new_buffered_reader(o BufferedReaderConfig) &BufferedReader {
|
||||
}
|
||||
|
||||
// read fufills the Reader interface
|
||||
pub fn (mut r BufferedReader) read(mut buf []u8) ?int {
|
||||
pub fn (mut r BufferedReader) read(mut buf []u8) !int {
|
||||
if r.end_of_stream {
|
||||
return none
|
||||
return IError(Eof{})
|
||||
}
|
||||
// read data out of the buffer if we dont have any
|
||||
if r.needs_fill() {
|
||||
if !r.fill_buffer() {
|
||||
// end of stream
|
||||
return none
|
||||
return IError(Eof{})
|
||||
}
|
||||
}
|
||||
read := copy(mut buf, r.buf[r.offset..r.len])
|
||||
if read == 0 {
|
||||
return none
|
||||
return IError(NotExpected{
|
||||
cause: 'invalid copy of buffer'
|
||||
code: -1
|
||||
})
|
||||
}
|
||||
r.offset += read
|
||||
return read
|
||||
|
||||
@@ -10,12 +10,12 @@ fn imin(a int, b int) int {
|
||||
return if a < b { a } else { b }
|
||||
}
|
||||
|
||||
fn (mut s StringReader) read(mut buf []u8) ?int {
|
||||
fn (mut s StringReader) read(mut buf []u8) !int {
|
||||
$if debug {
|
||||
eprintln('>>>> StringReader.read output buf.len: $buf.len')
|
||||
}
|
||||
if s.place > s.text.len + 1 {
|
||||
return none
|
||||
return IError(io.Eof{})
|
||||
}
|
||||
mut howmany := imin(buf.len, s.text.len - s.place)
|
||||
xxx := s.text[s.place..s.place + howmany].bytes()
|
||||
|
||||
@@ -12,9 +12,9 @@ pub mut:
|
||||
bytes []u8
|
||||
}
|
||||
|
||||
fn (mut b Buf) read(mut buf []u8) ?int {
|
||||
fn (mut b Buf) read(mut buf []u8) !int {
|
||||
if !(b.i < b.bytes.len) {
|
||||
return none
|
||||
return IError(io.Eof{})
|
||||
}
|
||||
n := copy(mut buf, b.bytes[b.i..])
|
||||
b.i += n
|
||||
|
||||
@@ -1,13 +1,32 @@
|
||||
module io
|
||||
|
||||
/// Eof error means that we reach the end of the stream.
|
||||
pub struct Eof {
|
||||
Error
|
||||
}
|
||||
|
||||
// NotExpected is a generic error that means that we receave a not expecte error.
|
||||
pub struct NotExpected {
|
||||
cause string
|
||||
code int
|
||||
}
|
||||
|
||||
fn (err NotExpected) msg() string {
|
||||
return err.cause
|
||||
}
|
||||
|
||||
fn (err NotExpected) code() int {
|
||||
return err.code
|
||||
}
|
||||
|
||||
// Reader represents a stream of data that can be read
|
||||
pub interface Reader {
|
||||
// read reads up to buf.len bytes and places
|
||||
// them into buf.
|
||||
// A type that implements this should return
|
||||
// `none` on end of stream (EOF) instead of just returning 0
|
||||
// `io.Eof` on end of stream (EOF) instead of just returning 0
|
||||
mut:
|
||||
read(mut buf []u8) ?int
|
||||
read(mut buf []u8) !int
|
||||
}
|
||||
|
||||
const (
|
||||
@@ -50,7 +69,7 @@ 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 { break }
|
||||
new_read := r.read(mut b[read..]) or { return none }
|
||||
read += new_read
|
||||
if new_read == 0 {
|
||||
break
|
||||
|
||||
@@ -7,9 +7,9 @@ mut:
|
||||
i int
|
||||
}
|
||||
|
||||
fn (mut b Buf) read(mut buf []u8) ?int {
|
||||
fn (mut b Buf) read(mut buf []u8) !int {
|
||||
if !(b.i < b.bytes.len) {
|
||||
return none
|
||||
return IError(Eof{})
|
||||
}
|
||||
n := copy(mut buf, b.bytes[b.i..])
|
||||
b.i += n
|
||||
@@ -44,9 +44,9 @@ mut:
|
||||
place int
|
||||
}
|
||||
|
||||
fn (mut s StringReader) read(mut buf []u8) ?int {
|
||||
fn (mut s StringReader) read(mut buf []u8) !int {
|
||||
if s.place >= s.text.len {
|
||||
return none
|
||||
return IError(Eof{})
|
||||
}
|
||||
read := copy(mut buf, s.text[s.place..].bytes())
|
||||
s.place += read
|
||||
|
||||
@@ -14,7 +14,7 @@ mut:
|
||||
w Writer
|
||||
}
|
||||
|
||||
pub fn (mut r ReaderWriterImpl) read(mut buf []u8) ?int {
|
||||
pub fn (mut r ReaderWriterImpl) read(mut buf []u8) !int {
|
||||
return r.r.read(mut buf)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user