io: deprecate io.make_reader (not needed anymore)

pull/10099/head
Delyan Angelov 2021-05-13 10:26:10 +03:00
parent 49deeac71e
commit 143c3d4bb4
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
11 changed files with 28 additions and 26 deletions

View File

@ -9,7 +9,7 @@ fn main() {
// Simple http HEAD request for a file
conn.write_string('GET /index.html HTTP/1.0\r\n\r\n') ?
// Wrap in a buffered reader
mut r := io.new_buffered_reader(reader: io.make_reader(conn))
mut r := io.new_buffered_reader(reader: conn)
for {
l := r.read_line() or { break }
println('$l')

View File

@ -28,7 +28,7 @@ fn read_from_string(text string, capacity int) []byte {
mut str := StringReader{
text: text
}
mut stream := io.new_buffered_reader(reader: io.make_reader(str), cap: capacity)
mut stream := io.new_buffered_reader(reader: str, cap: capacity)
//
mut buf := []byte{len: 1}
mut res := []byte{}

View File

@ -7,7 +7,7 @@ fn read_file(file string, cap int) []string {
defer {
f.close()
}
mut r := io.new_buffered_reader(reader: io.make_reader(f), cap: cap)
mut r := io.new_buffered_reader(reader: f, cap: cap)
for {
l := r.read_line() or { break }
lines << l

View File

@ -13,6 +13,8 @@ pub interface Reader {
// (e.g. for use in struct initialisation)
// (this shouldnt need to be a thing but until coercion gets made better
// it is required)
[deprecated: 'use just `x` instead of `io.make_reader(x)`. Interfaces are now checked against all types.']
[deprecated_after: '2021-05-27']
pub fn make_reader(r Reader) Reader {
return r
}

View File

@ -62,7 +62,7 @@ fn test_stringreader() {
mut s := StringReader{
text: text
}
mut r := new_buffered_reader(reader: make_reader(s))
mut r := new_buffered_reader(reader: s)
for i := 0; true; i++ {
if _ := r.read_line() {
} else {
@ -87,7 +87,7 @@ fn test_stringreader2() {
mut s := StringReader{
text: text
}
mut r := new_buffered_reader(reader: make_reader(s))
mut r := new_buffered_reader(reader: s)
for i := 0; true; i++ {
if _ := r.read_line() {
} else {
@ -98,7 +98,7 @@ fn test_stringreader2() {
if _ := r.read_line() {
assert false
}
leftover := read_all(reader: make_reader(r)) or {
leftover := read_all(reader: r) or {
assert false
panic('bad')
}
@ -112,7 +112,7 @@ fn test_leftover() {
mut s := StringReader{
text: text
}
mut r := new_buffered_reader(reader: make_reader(s))
mut r := new_buffered_reader(reader: s)
_ := r.read_line() or {
assert false
panic('bad')

View File

@ -103,7 +103,7 @@ fn (mut zftp FTP) read() ?(int, string) {
pub fn (mut zftp FTP) connect(ip string) ?bool {
zftp.conn = net.dial_tcp('$ip:21') ?
zftp.reader = io.new_buffered_reader(reader: io.make_reader(zftp.conn))
zftp.reader = io.new_buffered_reader(reader: zftp.conn)
code, _ := zftp.read() ?
if code == ftp.connected {
return true
@ -184,7 +184,7 @@ fn new_dtp(msg string) ?&DTP {
}
conn := net.dial_tcp('$ip:$port') or { return error('Cannot connect to the data channel') }
dtp.conn = conn
dtp.reader = io.new_buffered_reader(reader: io.make_reader(dtp.conn))
dtp.reader = io.new_buffered_reader(reader: dtp.conn)
return dtp
}

View File

@ -71,7 +71,7 @@ pub fn (mut c Client) reconnect() ? {
conn := net.dial_tcp('$c.server:$c.port') or { return error('Connecting to server failed') }
c.conn = conn
c.reader = io.new_buffered_reader(reader: io.make_reader(c.conn))
c.reader = io.new_buffered_reader(reader: c.conn)
c.expect_reply(.ready) or { return error('Received invalid response from server') }
c.send_ehlo() or { return error('Sending EHLO packet failed') }

View File

@ -9,13 +9,13 @@ const (
// read_line is a *simple*, *non customizable*, blocking line reader.
// It will *always* return a line, ending with CRLF, or just '', on EOF.
// NB: if you want more control over the buffer, please use a buffered IO
// reader instead: `io.new_buffered_reader({reader: io.make_reader(con)})`
// reader instead: `io.new_buffered_reader(reader: con)`
pub fn (mut con TcpConn) read_line() string {
mut buf := [max_read]byte{} // where C.recv will store the network data
mut buf := [net.max_read]byte{} // where C.recv will store the network data
mut res := '' // The final result, including the ending \n.
for {
mut line := '' // The current line. Can be a partial without \n in it.
n := C.recv(con.sock.handle, &buf[0], max_read - 1, msg_peek | msg_nosignal)
n := C.recv(con.sock.handle, &buf[0], net.max_read - 1, net.msg_peek | msg_nosignal)
if n == -1 {
return res
}
@ -48,7 +48,7 @@ pub fn (mut con TcpConn) read_line() string {
// recv returned a buffer without \n in it .
C.recv(con.sock.handle, &buf[0], n, msg_nosignal)
res += line
res += crlf
res += net.crlf
break
}
return res

View File

@ -19,9 +19,9 @@ fn setup() (&net.TcpListener, &net.TcpConn, &net.TcpConn) {
}
fn cleanup(mut server net.TcpListener, mut client net.TcpConn, mut socket net.TcpConn) {
server.close() or { }
client.close() or { }
socket.close() or { }
server.close() or {}
client.close() or {}
socket.close() or {}
}
fn test_socket() {
@ -74,9 +74,7 @@ fn test_socket_write_and_read() {
fn test_socket_read_line() {
mut server, mut client, mut socket := setup()
mut reader := io.new_buffered_reader(
reader: io.make_reader(client)
)
mut reader := io.new_buffered_reader(reader: client)
defer {
cleanup(mut server, mut client, mut socket)
}
@ -108,7 +106,7 @@ fn test_socket_write_fail_without_panic() {
// ensure that socket.write (i.e. done on the server side)
// continues to work, even when the client side has been disconnected
// this test is important for a stable long standing server
client.close() or { }
client.close() or {}
$if solaris {
return
}
@ -123,9 +121,7 @@ fn test_socket_write_fail_without_panic() {
fn test_socket_read_line_long_line_without_eol() {
mut server, mut client, mut socket := setup()
mut reader := io.new_buffered_reader(
reader: io.make_reader(client)
)
mut reader := io.new_buffered_reader(reader: client)
defer {
cleanup(mut server, mut client, mut socket)
}

View File

@ -20,7 +20,11 @@ fn (mut s StringReader) read(mut buf []byte) ?int {
}
fn reader(s string) &io.BufferedReader {
return io.new_buffered_reader(reader: io.make_reader(&StringReader{ text: s }))
return io.new_buffered_reader(
reader: &StringReader{
text: s
}
)
}
fn test_parse_request_not_http() {

View File

@ -337,7 +337,7 @@ fn handle_conn<T>(mut conn net.TcpConn, mut app T) {
defer {
conn.close() or {}
}
mut reader := io.new_buffered_reader(reader: io.make_reader(conn))
mut reader := io.new_buffered_reader(reader: conn)
defer {
reader.free()
}