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 // Simple http HEAD request for a file
conn.write_string('GET /index.html HTTP/1.0\r\n\r\n') ? conn.write_string('GET /index.html HTTP/1.0\r\n\r\n') ?
// Wrap in a buffered reader // 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 { for {
l := r.read_line() or { break } l := r.read_line() or { break }
println('$l') println('$l')

View File

@ -28,7 +28,7 @@ fn read_from_string(text string, capacity int) []byte {
mut str := StringReader{ mut str := StringReader{
text: text 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 buf := []byte{len: 1}
mut res := []byte{} mut res := []byte{}

View File

@ -7,7 +7,7 @@ fn read_file(file string, cap int) []string {
defer { defer {
f.close() 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 { for {
l := r.read_line() or { break } l := r.read_line() or { break }
lines << l lines << l

View File

@ -13,6 +13,8 @@ pub interface Reader {
// (e.g. for use in struct initialisation) // (e.g. for use in struct initialisation)
// (this shouldnt need to be a thing but until coercion gets made better // (this shouldnt need to be a thing but until coercion gets made better
// it is required) // 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 { pub fn make_reader(r Reader) Reader {
return r return r
} }

View File

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

View File

@ -103,7 +103,7 @@ fn (mut zftp FTP) read() ?(int, string) {
pub fn (mut zftp FTP) connect(ip string) ?bool { pub fn (mut zftp FTP) connect(ip string) ?bool {
zftp.conn = net.dial_tcp('$ip:21') ? 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() ? code, _ := zftp.read() ?
if code == ftp.connected { if code == ftp.connected {
return true 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') } conn := net.dial_tcp('$ip:$port') or { return error('Cannot connect to the data channel') }
dtp.conn = conn 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 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') } conn := net.dial_tcp('$c.server:$c.port') or { return error('Connecting to server failed') }
c.conn = conn 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.expect_reply(.ready) or { return error('Received invalid response from server') }
c.send_ehlo() or { return error('Sending EHLO packet failed') } 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. // read_line is a *simple*, *non customizable*, blocking line reader.
// It will *always* return a line, ending with CRLF, or just '', on EOF. // 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 // 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 { 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. mut res := '' // The final result, including the ending \n.
for { for {
mut line := '' // The current line. Can be a partial without \n in it. 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 { if n == -1 {
return res return res
} }
@ -48,7 +48,7 @@ pub fn (mut con TcpConn) read_line() string {
// recv returned a buffer without \n in it . // recv returned a buffer without \n in it .
C.recv(con.sock.handle, &buf[0], n, msg_nosignal) C.recv(con.sock.handle, &buf[0], n, msg_nosignal)
res += line res += line
res += crlf res += net.crlf
break break
} }
return res 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) { fn cleanup(mut server net.TcpListener, mut client net.TcpConn, mut socket net.TcpConn) {
server.close() or { } server.close() or {}
client.close() or { } client.close() or {}
socket.close() or { } socket.close() or {}
} }
fn test_socket() { fn test_socket() {
@ -74,9 +74,7 @@ fn test_socket_write_and_read() {
fn test_socket_read_line() { fn test_socket_read_line() {
mut server, mut client, mut socket := setup() mut server, mut client, mut socket := setup()
mut reader := io.new_buffered_reader( mut reader := io.new_buffered_reader(reader: client)
reader: io.make_reader(client)
)
defer { defer {
cleanup(mut server, mut client, mut socket) 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) // ensure that socket.write (i.e. done on the server side)
// continues to work, even when the client side has been disconnected // continues to work, even when the client side has been disconnected
// this test is important for a stable long standing server // this test is important for a stable long standing server
client.close() or { } client.close() or {}
$if solaris { $if solaris {
return return
} }
@ -123,9 +121,7 @@ fn test_socket_write_fail_without_panic() {
fn test_socket_read_line_long_line_without_eol() { fn test_socket_read_line_long_line_without_eol() {
mut server, mut client, mut socket := setup() mut server, mut client, mut socket := setup()
mut reader := io.new_buffered_reader( mut reader := io.new_buffered_reader(reader: client)
reader: io.make_reader(client)
)
defer { defer {
cleanup(mut server, mut client, mut socket) 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 { 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() { fn test_parse_request_not_http() {

View File

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