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

@@ -8,9 +8,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(io.Eof{})
}
max_bytes := 100
end := if s.place + max_bytes >= s.text.len { s.text.len } else { s.place + max_bytes }

View File

@@ -180,8 +180,8 @@ pub fn (mut s SSLConn) socket_read_into_ptr(buf_ptr &u8, len int) ?int {
return res
}
pub fn (mut s SSLConn) read(mut buffer []u8) ?int {
res := s.socket_read_into_ptr(&u8(buffer.data), buffer.len)?
pub fn (mut s SSLConn) read(mut buffer []u8) !int {
res := s.socket_read_into_ptr(&u8(buffer.data), buffer.len) or { return err }
return res
}

View File

@@ -1,6 +1,7 @@
module net
import time
import io
import strings
const (
@@ -132,8 +133,13 @@ pub fn (c TcpConn) read_ptr(buf_ptr &u8, len int) ?int {
return none
}
pub fn (c TcpConn) read(mut buf []u8) ?int {
return c.read_ptr(buf.data, buf.len)
pub fn (c TcpConn) read(mut buf []u8) !int {
return c.read_ptr(buf.data, buf.len) or {
return IError(io.NotExpected{
cause: 'unexpected error in `read_ptr` function'
code: -1
})
}
}
pub fn (mut c TcpConn) read_deadline() ?time.Time {

View File

@@ -9,10 +9,10 @@ fn (mut ws Client) socket_read(mut buffer []u8) ?int {
return error('socket_read: trying to read a closed socket')
}
if ws.is_ssl {
r := ws.ssl_conn.read(mut buffer)?
r := ws.ssl_conn.read(mut buffer) or { return none }
return r
} else {
r := ws.conn.read(mut buffer)?
r := ws.conn.read(mut buffer) or { return none }
return r
}
}