1
0
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:
Vincenzo Palazzo
2022-08-08 00:33:25 +01:00
committed by GitHub
parent 8c33a40c5a
commit b01f71d9da
13 changed files with 89 additions and 37 deletions

View File

@@ -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