Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/http_file_server/server_wbtest.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ async fn client(
let response = conn.get(path)
output_response(response, logger)
match handle_response {
None => logger <+ "\{conn.read_all().text()}"
None => logger <+ "\{conn.read_all_text()}"
Some(f) => f(conn)
}
}
Expand Down
7 changes: 5 additions & 2 deletions examples/line_processing/main.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,12 @@ async fn process_lines(
}
if keep {
if show_name {
output..write(name).write(":")
output..write_text(name).write_text(":")
}
output..write(line_prefix(line_no))..write(line).write("\n")
output
..write_text(line_prefix(line_no))
..write_text(line)
.write_text("\n")
}
line_no += 1
}
Expand Down
4 changes: 2 additions & 2 deletions examples/tcp_ping_pong/main.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ async fn server_main(server : @socket.TcpServer, println : Printer) -> Unit {
@async.sleep(10)
println("server received: \{@utf8.decode(msg)}")
let reply = "pong"
conn.write(reply)
conn.write_text(reply)
println("server sent: \{reply}")
if msg == b"exit" {
println("server initiate terminate")
Expand Down Expand Up @@ -58,7 +58,7 @@ async fn client(
// with server side "received connection" message
@async.sleep(10)
println("client \{id}: connection established")
conn.write(msg)
conn.write_text(msg)
println("client \{id} sent: \{msg}")
if conn.read_some() is Some(msg) {
@async.sleep(10)
Expand Down
4 changes: 2 additions & 2 deletions examples/websocket_echo_server/main.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ async fn start_echo_server(port? : Int = 9001) -> Unit {
let msg = ws.recv()
match msg.kind {
Text => {
let text = msg.read_all().text()
let text = msg.read_all_text()
println("Received text \{text.char_length()} chars")
ws.send_text(text)
}
Binary => {
let data = msg.read_all().binary()
let data = msg.read_all()
println("Received binary data (\{data.length()} bytes)")
ws.send_binary(data)
}
Expand Down
4 changes: 2 additions & 2 deletions examples/websocket_echo_server/server_wbtest.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ async test "test server" {
client.send_binary(b"abcd")
let msg = client.recv()
debug_inspect(msg.kind, content="Binary")
inspect(msg.read_all().text(), content="abcd")
inspect(msg.read_all_text(), content="abcd")
client.send_text("abcd")
let msg = client.recv()
debug_inspect(msg.kind, content="Text")
inspect(msg.read_all().text(), content="abcd")
inspect(msg.read_all_text(), content="abcd")
client.send_close()
}
}
18 changes: 9 additions & 9 deletions src/fs/README.mbt.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ async test "open file for reading" {
@fs.write_file(test_file, b"Hello, MoonBit!", create_mode=CreateOrTruncate)
let file = @fs.open(test_file, mode=ReadOnly)
defer file.close()
let content = file.read_all().text()
let content = file.read_all_text()
@fs.remove(test_file)
inspect(content, content="Hello, MoonBit!")
}
Expand All @@ -62,7 +62,7 @@ async test "open with append mode" {
let file = @fs.open(test_file, mode=WriteOnly, append=true)
file.write(b"Second line\n")
file.close()
let content = @fs.read_file(test_file).text()
let content = @utf8.decode(@fs.read_file(test_file))
@fs.remove(test_file)
inspect(content, content="First line\nSecond line\n")
}
Expand Down Expand Up @@ -101,7 +101,7 @@ async test "read_file - read entire file" {
@fs.write_file(test_file, b"Hello, MoonBit!", create_mode=CreateOrTruncate)
let content = @fs.read_file(test_file)
@fs.remove(test_file)
inspect(content.text(), content="Hello, MoonBit!")
inspect(@utf8.decode(content), content="Hello, MoonBit!")
}

///|
Expand All @@ -127,7 +127,7 @@ async test "read_all from file" {
let data = file.read_all()
file.close()
@fs.remove(test_file)
inspect(data.text(), content="Complete content")
inspect(@utf8.decode(data), content="Complete content")
}

///|
Expand Down Expand Up @@ -156,7 +156,7 @@ Write data to files using various methods:
async test "write_file - write entire file" {
let test_file = "_build/test_write.txt"
@fs.write_file(test_file, b"File content", create_mode=CreateOrTruncate)
let content = @fs.read_file(test_file).text()
let content = @utf8.decode(@fs.read_file(test_file))
@fs.remove(test_file)
inspect(content, content="File content")
}
Expand All @@ -172,7 +172,7 @@ async test "write with sync modes" {
sync=Data,
create_mode=CreateOrTruncate,
)
let content = @fs.read_file(test_file).text()
let content = @utf8.decode(@fs.read_file(test_file))
@fs.remove(test_file)
inspect(content, content="Synced data")
}
Expand All @@ -185,7 +185,7 @@ async test "write using File methods" {
file.write(b"Line 1\n")
file.write(b"Line 2\n")
file.close()
let content = @fs.read_file(test_file).text()
let content = @utf8.decode(@fs.read_file(test_file))
@fs.remove(test_file)
inspect(content, content="Line 1\nLine 2\n")
}
Expand Down Expand Up @@ -241,12 +241,12 @@ async test "write at specific position" {
{
let file = @fs.open(test_file, mode=WriteOnly, create_mode=CreateOrTruncate)
defer file.close()
file.write("abcdef")
file.write_text("abcdef")
file.write_at(b"CD", position=2)
}

// read 3 bytes at position 5
inspect(@fs.read_file(test_file).text(), content="abCDef")
inspect(@utf8.decode(@fs.read_file(test_file)), content="abCDef")
@fs.remove(test_file)
}

Expand Down
2 changes: 1 addition & 1 deletion src/fs/access_test.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ async test "chmod" {
}
let path = "_build/chmod_test"
@async.with_task_group() <| group => {
@fs.write_file(path, "abcd", create_mode=CreateNew, permission=0o444)
@fs.write_file(path, b"abcd", create_mode=CreateNew, permission=0o444)
group.add_defer(() => @fs.remove(path))
inspect(@fs.can_read(path), content="true")
inspect(@fs.can_write(path), content="false")
Expand Down
26 changes: 13 additions & 13 deletions src/fs/create_test.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ async test "basic create" {
let result = {
let r = @fs.open(path, mode=ReadOnly)
defer r.close()
r.read_all().text()
r.read_all_text()
}
@fs.remove(path)
inspect(
Expand Down Expand Up @@ -55,7 +55,7 @@ async test "create or append" {
let result = {
let r = @fs.open(path, mode=ReadOnly)
defer r.close()
r.read_all().text()
r.read_all_text()
}
@fs.remove(path)
inspect(result, content="abcdefgh")
Expand All @@ -65,7 +65,7 @@ async test "create or append" {
async test "wrapper test" {
let path = "_build/fs_wrapper_test"
@fs.write_file(path, b"abcd", create_mode=CreateOrTruncate)
let result = @fs.read_file(path).text()
let result = @utf8.decode(@fs.read_file(path))
@fs.remove(path)
inspect(result, content="abcd")
}
Expand All @@ -83,36 +83,36 @@ async test "create_mode test" {
let file = @fs.create(path, allow_existing=false)
defer file.close()
group.add_defer(() => @fs.remove(path))
file.write("abcd")
file.write_text("abcd")
}
inspect(@fs.read_file(path).text(), content="abcd")
inspect(@utf8.decode(@fs.read_file(path)), content="abcd")
@test_util.assert_raise_async(() => @fs.create(path, allow_existing=false))
@test_util.assert_raise_async <| () => {
@fs.open(path, mode=WriteOnly, create_mode=CreateNew)
}
{
let file = @fs.open(path, mode=WriteOnly, create_mode=OpenExisting)
defer file.close()
file.write("AB")
file.write_text("AB")
}
inspect(@fs.read_file(path).text(), content="ABcd")
inspect(@utf8.decode(@fs.read_file(path)), content="ABcd")
{
let file = @fs.open(path, mode=WriteOnly, create_mode=OpenOrCreate)
defer file.close()
file.write("XY")
file.write_text("XY")
}
inspect(@fs.read_file(path).text(), content="XYcd")
inspect(@utf8.decode(@fs.read_file(path)), content="XYcd")
{
let file = @fs.open(path, mode=WriteOnly, create_mode=CreateOrTruncate)
defer file.close()
file.write("12")
file.write_text("12")
}
inspect(@fs.read_file(path).text(), content="12")
inspect(@utf8.decode(@fs.read_file(path)), content="12")
{
let file = @fs.open(path, mode=WriteOnly, create_mode=TruncateExisting)
defer file.close()
file.write("34")
file.write_text("34")
}
inspect(@fs.read_file(path).text(), content="34")
inspect(@utf8.decode(@fs.read_file(path)), content="34")
}
}
8 changes: 4 additions & 4 deletions src/fs/cursor_test.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
///|
async test "sequential read/write have separated cursor" {
let path = "_build/read_write_cursor_test"
@fs.write_file(path, "abcd", create_mode=CreateOrTruncate, sync=Full)
@fs.write_file(path, b"abcd", create_mode=CreateOrTruncate, sync=Full)
let file = @fs.open(path, mode=ReadWrite)
defer file.close()
file.write(b"AB")
Expand All @@ -26,13 +26,13 @@ async test "sequential read/write have separated cursor" {
///|
async test "append mode only affect writing" {
let path = "_build/append_cursor_test"
@fs.write_file(path, "abcd", create_mode=CreateOrTruncate, sync=Full)
@fs.write_file(path, b"abcd", create_mode=CreateOrTruncate, sync=Full)
let file = @fs.open(path, append=true, mode=ReadWrite)
defer file.close()
// the read cursor should not be affected by `append`
json_inspect(file.read_exactly(2), content="ab")
// writing should append to EOF automatically
file.write("ef")
file.write_text("ef")
// read cursor is unaffected by append write
json_inspect(file.read_exactly(2), content="cd")
json_inspect(file.read_exactly(2), content="ef")
Expand All @@ -41,7 +41,7 @@ async test "append mode only affect writing" {
///|
async test "append mode file does not support random access write" {
let path = "_build/append_random_access_test"
@fs.write_file(path, "abcd", create_mode=CreateOrTruncate, sync=Full)
@fs.write_file(path, b"abcd", create_mode=CreateOrTruncate, sync=Full)
let file = @fs.open(path, append=true, mode=ReadWrite)
defer file.close()
// random access read is not affected
Expand Down
6 changes: 3 additions & 3 deletions src/fs/file.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ pub async fn File::ctime(self : File) -> (Int64, Int) {
pub async fn read_file(
path : StringView,
sync_timestamp? : Bool = false,
) -> &@io.Data {
) -> Bytes {
let file = open(path, mode=ReadOnly)
defer file.close()
let result = file.read_all()
Expand All @@ -491,7 +491,7 @@ pub async fn read_file(
#label_migration(truncate, fill=false, msg="the option `truncate` is deprecated, use `create_mode` instead")
pub async fn write_file(
path : StringView,
content : &@io.Data,
content : BytesView,
create_mode? : CreateMode,
permission? : Int,
sync? : SyncMode = Data,
Expand Down Expand Up @@ -523,5 +523,5 @@ pub async fn write_file(
permission~,
)
defer file.close()
file.write(content.binary())
file.write(content)
}
4 changes: 2 additions & 2 deletions src/fs/lock_test.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ async test "flock advisory" {
let log = []
let file_name = "_build/flock_advisory"
@async.with_task_group() <| group => {
@fs.write_file(file_name, create_mode=CreateOrTruncate, "abcd")
@fs.write_file(file_name, create_mode=CreateOrTruncate, b"abcd")
let file = @fs.open(file_name, mode=ReadWrite)
defer file.close()
group.add_defer(() => @fs.remove(file_name))
Expand All @@ -342,7 +342,7 @@ async test "flock advisory" {
)
@async.sleep(250)
log.push("attempting to read")
log.push("read from file: \{file.read_all().text()}")
log.push("read from file: \{file.read_all_text()}")
}
json_inspect(log, content=[
"child: acquired lock", "attempting to read", "read from file: abcd", "child: releasing lock",
Expand Down
6 changes: 3 additions & 3 deletions src/fs/mkdir_test.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
async test "mkdir" {
let path = "_build/mkdir_test"
@fs.mkdir(path)
@fs.write_file("\{path}/file", "", create_mode=CreateOrTruncate)
@fs.write_file("\{path}/file", b"", create_mode=CreateOrTruncate)
let result = @fs.readdir(path, sort=true)
@fs.remove("\{path}/file")
@fs.rmdir(path)
Expand All @@ -28,9 +28,9 @@ async test "rmdir recursive" {
guard @env.current_dir() is Some(cwd)
let path = "_build/rmdir_test"
@fs.mkdir(path, permission=0o755)
@fs.write_file("\{path}/file", "", create_mode=CreateOrTruncate)
@fs.write_file("\{path}/file", b"", create_mode=CreateOrTruncate)
@fs.mkdir("\{path}/inner")
@fs.write_file("\{path}/inner/file", "", create_mode=CreateOrTruncate)
@fs.write_file("\{path}/inner/file", b"", create_mode=CreateOrTruncate)
@fs.symlink("\{path}/symlink", target="\{cwd}/_build")
@test_util.assert_raise_async(() => @fs.rmdir(path))
@fs.rmdir(path, recursive=true)
Expand Down
12 changes: 6 additions & 6 deletions src/fs/named_pipe_test.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,17 @@ async test "named fifo" {
defer r.close()
inspect(r.read_until("\n").unwrap_or(""), content="abcd")
debug_inspect(
@async.with_timeout_opt(250, () => r.read_all().text()),
@async.with_timeout_opt(250, () => r.read_all_text()),
content="None",
)
inspect(r.read_all().text(), content="efgh")
inspect(r.read_all_text(), content="efgh")
}
group.spawn_bg() <| () => {
let w = @fs.open(path, mode=WriteOnly)
defer w.close()
w.write("abcd\n")
w.write_text("abcd\n")
@async.sleep(500)
w.write("efgh")
w.write_text("efgh")
}
}
}
Expand All @@ -101,10 +101,10 @@ async test "named pipe" {
defer r.close()
inspect(r.read_until("\n").unwrap_or(""), content="abcd")
debug_inspect(
@async.with_timeout_opt(250, () => r.read_all().text()),
@async.with_timeout_opt(250, () => r.read_all_text()),
content="None",
)
inspect(r.read_all().text(), content="efgh")
inspect(r.read_all_text(), content="efgh")
}
group.spawn_bg() <| () => {
// wait for the client to connect
Expand Down
4 changes: 2 additions & 2 deletions src/fs/pkg.generated.mbti
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub async fn open(StringView, mode~ : Mode, sync? : SyncMode, append? : Bool, cr

pub async fn opendir(StringView) -> Directory

pub async fn read_file(StringView, sync_timestamp? : Bool) -> &@io.Data
pub async fn read_file(StringView, sync_timestamp? : Bool) -> Bytes

pub async fn readdir(StringView, include_hidden? : Bool, include_special? : Bool, sort? : Bool) -> Array[String]

Expand All @@ -55,7 +55,7 @@ pub async fn walk(StringView, async (String, Array[String]) -> Unit, exclude? :

#label_migration(create, fill=false, msg="the option `create` is deprecated, use `create_mode` and `permission` instead")
#label_migration(truncate, fill=false, msg="the option `truncate` is deprecated, use `create_mode` instead")
pub async fn write_file(StringView, &@io.Data, create_mode? : CreateMode, permission? : Int, sync? : SyncMode, append? : Bool, create? : Int, truncate? : Bool) -> Unit
pub async fn write_file(StringView, BytesView, create_mode? : CreateMode, permission? : Int, sync? : SyncMode, append? : Bool, create? : Int, truncate? : Bool) -> Unit

// Errors

Expand Down
Loading
Loading