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:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user