Skip to content

Commit 41208f9

Browse files
committed
add @io.Data and use it for convenient conversion of data format
1 parent 1a43fa5 commit 41208f9

29 files changed

+216
-190
lines changed

examples/tcp_server_benchmark/moon.pkg.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"is-main": true,
33
"import": [
44
"moonbitlang/async",
5+
"moonbitlang/async/io",
56
"moonbitlang/async/socket"
67
]
78
}

src/fs/create_test.mbt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ async test "basic create" {
2323
let result = {
2424
let r = @fs.open(path, mode=ReadOnly)
2525
defer r.close()
26-
@bytes_util.ascii_to_string(r.read_all())
26+
r.read_all().text()
2727
}
2828
@fs.remove(path)
2929
inspect(
@@ -49,7 +49,7 @@ async test "create or append" {
4949
let result = {
5050
let r = @fs.open(path, mode=ReadOnly)
5151
defer r.close()
52-
r.read_all() |> @bytes_util.ascii_to_string
52+
r.read_all().text()
5353
}
5454
@fs.remove(path)
5555
inspect(result, content="abcdefgh")
@@ -59,7 +59,7 @@ async test "create or append" {
5959
async test "wrapper test" {
6060
let path = "fs_wrapper_test"
6161
@fs.write_file(path, b"abcd", create=0o644)
62-
let result = @fs.read_file(path) |> @bytes_util.ascii_to_string
62+
let result = @fs.read_file(path).text()
6363
@fs.remove(path)
6464
inspect(result, content="abcd")
6565
}

src/fs/fs.mbt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ pub impl @io.Reader for File with read_all(self) {
182182
let size = self.size().to_int()
183183
let result = FixedArray::make(size, b'0')
184184
guard self.read(result) == size
185-
result.unsafe_reinterpret_as_bytes()
185+
result.unsafe_reinterpret_as_bytes() as &@io.Data
186186
}
187187
_ => @io.Reader::read_all(DummyFile(self))
188188
}
@@ -282,7 +282,7 @@ pub fn File::kind(self : File) -> FileKind {
282282

283283
///|
284284
/// Read the contents of the file located at `path`.
285-
pub async fn read_file(path : String) -> Bytes {
285+
pub async fn read_file(path : String) -> &@io.Data {
286286
let file = open(path, mode=ReadOnly)
287287
defer file.close()
288288
file.read_all()
@@ -292,7 +292,7 @@ pub async fn read_file(path : String) -> Bytes {
292292
/// Read the contents of a text file located at `path`.
293293
pub async fn read_text_file(path : String, encoding~ : @io.Encoding) -> String {
294294
guard encoding is UTF8
295-
@encoding/utf8.decode(read_file(path))
295+
read_file(path).text()
296296
}
297297

298298
///|

src/fs/pkg.generated.mbti

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ async fn open(String, mode~ : Mode, sync? : SyncMode, append? : Bool, create? :
2424

2525
fn opendir(String) -> Directory raise
2626

27-
async fn read_file(String) -> Bytes
27+
async fn read_file(String) -> &@io.Data
2828

2929
async fn read_text_file(String, encoding~ : @io.Encoding) -> String
3030

src/fs/read_all_test.mbt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ async test "read_all basic" {
1818
let r = @fs.open(path, mode=ReadOnly)
1919
defer r.close()
2020
inspect(
21-
r.read_all() |> @bytes_util.ascii_to_string,
21+
r.read_all().text(),
2222
content=(
2323
#|
2424
#| Apache License

src/fs/seek_test.mbt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ async test "basic seek" {
2828
let r = @fs.open(path, mode=ReadOnly)
2929
defer r.close()
3030
inspect(r.size(), content="6")
31-
inspect(@bytes_util.ascii_to_string(r.read_all()), content="abcdef")
31+
inspect(r.read_all().text(), content="abcdef")
3232
}
3333
// update content
3434
{
@@ -48,7 +48,7 @@ async test "basic seek" {
4848
{
4949
let r = @fs.open(path, mode=ReadOnly)
5050
defer r.close()
51-
inspect(@bytes_util.ascii_to_string(r.read_all()), content="123456")
51+
inspect(r.read_all().text(), content="123456")
5252
}
5353
@fs.remove(path)
5454
}

src/http/client.mbt

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,6 @@ pub impl @io.Reader for Client with read(
7979
self.reader.read(buf, offset~, max_len~)
8080
}
8181

82-
///|
83-
/// Read the whole response body from a HTTP client.
84-
/// Must be called after `end_request`.
85-
pub async fn Client::read_all(self : Client) -> &Body {
86-
@io.Reader::read_all(self.reader)
87-
}
88-
8982
///|
9083
/// Write data to the body of the request currently being sent.
9184
/// Must be called after `send_request`.
@@ -165,11 +158,11 @@ pub async fn Client::get(
165158
self : Client,
166159
path : String,
167160
extra_headers? : Map[String, String] = {},
168-
body? : &Body,
161+
body? : &@io.Data,
169162
) -> Response {
170163
self.request(Get, path, extra_headers~)
171164
if body is Some(body) {
172-
self.write(body.to_bytes())
165+
self.write(body)
173166
}
174167
self.end_request()
175168
}
@@ -179,22 +172,19 @@ pub async fn Client::get(
179172
pub async fn Client::put(
180173
self : Client,
181174
path : String,
182-
body : &Body,
175+
body : &@io.Data,
183176
extra_headers? : Map[String, String] = {},
184177
) -> Response {
185-
self..request(Put, path, extra_headers~)..write(body.to_bytes()).end_request()
178+
self..request(Put, path, extra_headers~)..write(body).end_request()
186179
}
187180

188181
///|
189182
/// Perform a `POST` request to the server, see `Client::request` for more details.
190183
pub async fn Client::post(
191184
self : Client,
192185
path : String,
193-
body : &Body,
186+
body : &@io.Data,
194187
extra_headers? : Map[String, String] = {},
195188
) -> Response {
196-
self
197-
..request(Post, path, extra_headers~)
198-
..write(body.to_bytes())
199-
.end_request()
189+
self..request(Post, path, extra_headers~)..write(body).end_request()
200190
}

src/http/parser_wbtest.mbt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ async test "read_request basic" {
4747
let input = Reader::new(r)
4848
let request = input.read_request()
4949
log_request(request, log)
50-
log.write_string(input.read_all() |> @bytes_util.ascii_to_string)
50+
log.write_string(input.read_all().text())
5151
})
5252
inspect(
5353
log.to_string(),
@@ -78,7 +78,7 @@ async test "read_request fixed body" {
7878
let input = Reader::new(r)
7979
let request = input.read_request()
8080
log_request(request, log)
81-
log.write_string(input.read_all() |> @bytes_util.ascii_to_string)
81+
log.write_string(input.read_all().text())
8282
})
8383
inspect(
8484
log.to_string(),
@@ -114,7 +114,7 @@ async test "read_request chunked" {
114114
let input = Reader::new(r)
115115
let request = input.read_request()
116116
log_request(request, log)
117-
log.write_string(input.read_all() |> @bytes_util.ascii_to_string)
117+
log.write_string(input.read_all().text())
118118
})
119119
inspect(
120120
log.to_string(),
@@ -215,11 +215,11 @@ async test "multiple request" {
215215
let request = input.read_request()
216216
log_request(request, log)
217217
log
218-
..write_string(input.read_all() |> @bytes_util.ascii_to_string)
218+
..write_string(input.read_all().text())
219219
..write_string("\n\n=========\n\n")
220220
let request = input.read_request()
221221
log_request(request, log)
222-
log.write_string(input.read_all() |> @bytes_util.ascii_to_string)
222+
log.write_string(input.read_all().text())
223223
})
224224
inspect(
225225
log.to_string(),
@@ -260,7 +260,7 @@ async test "read_response basic" {
260260
let input = Reader::new(r)
261261
let response = input.read_response()
262262
log_response(response, log)
263-
log.write_string(input.read_all() |> @bytes_util.ascii_to_string)
263+
log.write_string(input.read_all().text())
264264
})
265265
inspect(
266266
log.to_string(),

src/http/pkg.generated.mbti

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@
22
package "moonbitlang/async/http"
33

44
import(
5+
"moonbitlang/async/io"
56
"moonbitlang/async/socket"
67
)
78

89
// Values
9-
async fn get(String, headers? : Map[String, String], port? : Int, body? : &Body) -> (Response, &Body)
10+
async fn get(String, headers? : Map[String, String], port? : Int, body? : &@io.Data) -> (Response, &@io.Data)
1011

11-
async fn post(String, &Body, headers? : Map[String, String], port? : Int) -> (Response, &Body)
12+
async fn post(String, &@io.Data, headers? : Map[String, String], port? : Int) -> (Response, &@io.Data)
1213

13-
async fn put(String, &Body, headers? : Map[String, String], port? : Int) -> (Response, &Body)
14+
async fn put(String, &@io.Data, headers? : Map[String, String], port? : Int) -> (Response, &@io.Data)
1415

1516
// Errors
1617
pub suberror HttpProtocolError {
@@ -32,14 +33,13 @@ fn Client::close(Self) -> Unit
3233
async fn Client::connect(String, headers? : Map[String, String], protocol? : Protocol, port? : Int) -> Self
3334
async fn Client::end_request(Self) -> Response
3435
async fn Client::flush(Self) -> Unit
35-
async fn Client::get(Self, String, extra_headers? : Map[String, String], body? : &Body) -> Response
36-
async fn Client::post(Self, String, &Body, extra_headers? : Map[String, String]) -> Response
37-
async fn Client::put(Self, String, &Body, extra_headers? : Map[String, String]) -> Response
38-
async fn Client::read_all(Self) -> &Body
36+
async fn Client::get(Self, String, extra_headers? : Map[String, String], body? : &@io.Data) -> Response
37+
async fn Client::post(Self, String, &@io.Data, extra_headers? : Map[String, String]) -> Response
38+
async fn Client::put(Self, String, &@io.Data, extra_headers? : Map[String, String]) -> Response
3939
async fn Client::request(Self, RequestMethod, String, extra_headers? : Map[String, String]) -> Unit
4040
async fn Client::skip_response_body(Self) -> Unit
41-
impl @moonbitlang/async/io.Reader for Client
42-
impl @moonbitlang/async/io.Writer for Client
41+
impl @io.Reader for Client
42+
impl @io.Writer for Client
4343

4444
pub(all) enum Protocol {
4545
Http
@@ -77,22 +77,13 @@ fn ServerConnection::close(Self) -> Unit
7777
async fn ServerConnection::end_response(Self) -> Unit
7878
async fn ServerConnection::flush(Self) -> Unit
7979
fn ServerConnection::new(@socket.TCP, headers? : Map[String, String]) -> Self
80-
async fn ServerConnection::read_all(Self) -> &Body
8180
async fn ServerConnection::read_request(Self) -> Request
8281
async fn ServerConnection::send_response(Self, Int, String, extra_headers? : Map[String, String]) -> Unit
8382
async fn ServerConnection::skip_request_body(Self) -> Unit
84-
impl @moonbitlang/async/io.Reader for ServerConnection
85-
impl @moonbitlang/async/io.Writer for ServerConnection
86-
87-
async fn Body::binary(&Self) -> Bytes
88-
async fn Body::json(&Self) -> Json
89-
async fn Body::text(&Self) -> String
83+
impl @io.Reader for ServerConnection
84+
impl @io.Writer for ServerConnection
9085

9186
// Type aliases
9287

9388
// Traits
94-
trait Body
95-
impl Body for String
96-
impl Body for Bytes
97-
impl Body for Json
9889

src/http/request.mbt

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ async fn perform_request(
3737
uri : String,
3838
meth : RequestMethod,
3939
headers : Map[String, String],
40-
body : &Body,
40+
body : &@io.Data,
4141
port? : Int,
42-
) -> (Response, &Body) {
42+
) -> (Response, &@io.Data) {
4343
guard uri.find("://") is Some(protocol_len) else { raise InvalidFormat }
4444
let protocol = match uri[:protocol_len] {
4545
"http" => Http
@@ -63,7 +63,7 @@ async fn perform_request(
6363
let path = if path == "" { "/" } else { path }
6464
let client = Client::connect(host, headers~, protocol~, port~)
6565
defer client.close()
66-
client..request(meth, path)..write(body.to_bytes())
66+
client..request(meth, path)..write(body)
6767
let response = client.end_request()
6868
(response, client.read_all())
6969
}
@@ -81,29 +81,29 @@ pub async fn get(
8181
uri : String,
8282
headers? : Map[String, String] = {},
8383
port? : Int,
84-
body? : &Body = b"",
85-
) -> (Response, &Body) {
84+
body? : &@io.Data = b"",
85+
) -> (Response, &@io.Data) {
8686
perform_request(uri, Get, headers, body, port?)
8787
}
8888

8989
///|
9090
/// Similar to `get`, but performs a `PUT` request instead.
9191
pub async fn put(
9292
uri : String,
93-
content : &Body,
93+
content : &@io.Data,
9494
headers? : Map[String, String] = {},
9595
port? : Int,
96-
) -> (Response, &Body) {
96+
) -> (Response, &@io.Data) {
9797
perform_request(uri, Put, headers, content, port?)
9898
}
9999

100100
///|
101101
/// Similar to `get`, but performs a `POST` request instead.
102102
pub async fn post(
103103
uri : String,
104-
content : &Body,
104+
content : &@io.Data,
105105
headers? : Map[String, String] = {},
106106
port? : Int,
107-
) -> (Response, &Body) {
107+
) -> (Response, &@io.Data) {
108108
perform_request(uri, Post, headers, content, port?)
109109
}

0 commit comments

Comments
 (0)