Skip to content

Commit 8ae1ee8

Browse files
committed
move stdio to dedicated package, handle case where stdio is redirected to file
1 parent 5e0cdae commit 8ae1ee8

File tree

13 files changed

+245
-77
lines changed

13 files changed

+245
-77
lines changed

examples/cat/main.mbt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,15 @@
1616
async fn main {
1717
let args = @env.args()
1818
match args {
19-
[] | [_] => @pipe.stdout.write_reader(@pipe.stdin)
19+
[] | [_] => @stdio.stdout.write_reader(@stdio.stdin)
2020
[_, .. files] =>
2121
for file in files {
2222
if file == "-" {
23-
@pipe.stdout.write_reader(@pipe.stdin)
23+
@stdio.stdout.write_reader(@stdio.stdin)
2424
} else {
2525
let file = @fs.open(file, mode=ReadOnly)
26-
@pipe.stdout.write_reader(file)
26+
defer file.close()
27+
@stdio.stdout.write_reader(file)
2728
}
2829
}
2930
}

examples/cat/moon.pkg.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"moonbitlang/async",
44
"moonbitlang/async/io",
55
"moonbitlang/async/fs",
6-
"moonbitlang/async/pipe"
6+
"moonbitlang/async/stdio"
77
],
88
"is-main": true
99
}

src/fs/dir_test.mbt

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,12 @@ async test "read_all" {
1818
defer dir.close()
1919
let list = dir.read_all()
2020
list.sort()
21-
@json.inspect(list, content=(["fs.mbt","stub.c","dir.mbt","README.md","utils.mbt","dir_test.mbt","eof_test.mbt","README.mbt.md","moon.pkg.json","seek_test.mbt","stat_test.mbt","walk_test.mbt","mkdir_test.mbt","access_test.mbt","create_test.mbt","read_all_test.mbt","realpath_test.mbt","pkg.generated.mbti","text_file_test.mbt","timestamp_test.mbt"]))
21+
@json.inspect(list, content=[
22+
"fs.mbt", "stub.c", "dir.mbt", "README.md", "utils.mbt", "dir_test.mbt", "eof_test.mbt",
23+
"README.mbt.md", "moon.pkg.json", "seek_test.mbt", "stat_test.mbt", "walk_test.mbt",
24+
"mkdir_test.mbt", "access_test.mbt", "create_test.mbt", "read_all_test.mbt",
25+
"realpath_test.mbt", "pkg.generated.mbti", "text_file_test.mbt", "timestamp_test.mbt",
26+
])
2227
}
2328

2429
///|
@@ -41,5 +46,12 @@ async test "as_dir" {
4146
assert_eq(kind, @fs.kind(path))
4247
kinds.push("\{file}: \{kind}")
4348
}
44-
@json.inspect(kinds, content=(["fs.mbt: Regular","stub.c: Regular","dir.mbt: Regular","README.md: Regular","utils.mbt: Regular","dir_test.mbt: Regular","eof_test.mbt: Regular","README.mbt.md: Regular","moon.pkg.json: Regular","seek_test.mbt: Regular","stat_test.mbt: Regular","walk_test.mbt: Regular","mkdir_test.mbt: Regular","access_test.mbt: Regular","create_test.mbt: Regular","read_all_test.mbt: Regular","realpath_test.mbt: Regular","pkg.generated.mbti: Regular","text_file_test.mbt: Regular","timestamp_test.mbt: Regular"]))
49+
@json.inspect(kinds, content=[
50+
"fs.mbt: Regular", "stub.c: Regular", "dir.mbt: Regular", "README.md: Regular",
51+
"utils.mbt: Regular", "dir_test.mbt: Regular", "eof_test.mbt: Regular", "README.mbt.md: Regular",
52+
"moon.pkg.json: Regular", "seek_test.mbt: Regular", "stat_test.mbt: Regular",
53+
"walk_test.mbt: Regular", "mkdir_test.mbt: Regular", "access_test.mbt: Regular",
54+
"create_test.mbt: Regular", "read_all_test.mbt: Regular", "realpath_test.mbt: Regular",
55+
"pkg.generated.mbti: Regular", "text_file_test.mbt: Regular", "timestamp_test.mbt: Regular",
56+
])
4557
}

src/fs/fs.mbt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,8 +287,8 @@ pub fn File::as_dir(self : File) -> Directory raise {
287287
///|
288288
fn FileKind::can_poll(self : FileKind) -> Bool {
289289
match self {
290-
Socket | Pipe => true
291-
Unknown | Regular | Directory | SymLink | BlockDevice | CharDevice => false
290+
Socket | Pipe | CharDevice => true
291+
Unknown | Regular | Directory | SymLink | BlockDevice => false
292292
}
293293
}
294294

src/pipe/moon.pkg.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"import": [
33
"moonbitlang/async/internal/event_loop",
44
"moonbitlang/async/internal/fd_util",
5+
"moonbitlang/async/stdio",
56
"moonbitlang/async/io"
67
],
78
"test-import": [

src/pipe/pipe.mbt

Lines changed: 6 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -43,79 +43,16 @@ pub fn pipe() -> (PipeRead, PipeWrite) raise {
4343
}
4444

4545
///|
46-
/// The standard input treated as a pipe.
47-
/// Using this value will result in standard input being set to non-blocking mode.
48-
/// `@async.with_event_loop` will reset standard input
49-
/// back to its original state on exit.
50-
pub let stdin : PipeRead = {
51-
try @fd_util.fd_is_nonblocking(0, context="initialize `@pipe.stdin`") catch {
52-
err => abort(err.to_string())
53-
} noraise {
54-
true => ()
55-
false => {
56-
fn init() raise {
57-
@fd_util.set_nonblocking(0, context="initialize `@pipe.stdin`")
58-
}
59-
60-
fn exit() raise {
61-
@fd_util.set_nonblocking(0, context="restore `@pipe.stdin`")
62-
}
63-
64-
@event_loop.register_hook(init~, exit~)
65-
}
66-
}
67-
PipeRead(0)
68-
}
46+
#deprecated("use `@stdio.stdin` instead")
47+
pub let stdin : PipeRead = @stdio.stdin.fd()
6948

7049
///|
71-
/// The standard output treated as a pipe.
72-
/// Using this value will result in standard output being set to non-blocking mode.
73-
/// `@async.with_event_loop` will reset standard output
74-
/// back to its original state on exit.
75-
pub let stdout : PipeWrite = {
76-
try @fd_util.fd_is_nonblocking(1, context="initialize `@pipe.stdout`") catch {
77-
err => abort(err.to_string())
78-
} noraise {
79-
true => ()
80-
false => {
81-
fn init() raise {
82-
@fd_util.set_nonblocking(1, context="initialize `@pipe.stdout`")
83-
}
84-
85-
fn exit() raise {
86-
@fd_util.set_nonblocking(1, context="restore `@pipe.stdout`")
87-
}
88-
89-
@event_loop.register_hook(init~, exit~)
90-
}
91-
}
92-
PipeWrite(1)
93-
}
50+
#deprecated("use `@stdio.stdout` instead")
51+
pub let stdout : PipeWrite = @stdio.stdout.fd()
9452

9553
///|
96-
/// The standard error treated as a pipe.
97-
/// Using this value will result in standard error being set to non-blocking mode.
98-
/// `@async.with_event_loop` will reset standard error
99-
/// back to its original state on exit.
100-
pub let stderr : PipeWrite = {
101-
try @fd_util.fd_is_nonblocking(2, context="initialize `@pipe.stderr`") catch {
102-
err => abort(err.to_string())
103-
} noraise {
104-
true => ()
105-
false => {
106-
fn init() raise {
107-
@fd_util.set_nonblocking(2, context="initialize `@pipe.stderr`")
108-
}
109-
110-
fn exit() raise {
111-
@fd_util.set_nonblocking(2, context="restore `@pipe.stderr`")
112-
}
113-
114-
@event_loop.register_hook(init~, exit~)
115-
}
116-
}
117-
PipeWrite(2)
118-
}
54+
#deprecated("use `@stdio.stderr` instead")
55+
pub let stderr : PipeWrite = @stdio.stderr.fd()
11956

12057
///|
12158
pub fn PipeRead::close(self : PipeRead) -> Unit {

src/pipe/pkg.generated.mbti

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,13 @@ import(
88
// Values
99
fn pipe() -> (PipeRead, PipeWrite) raise
1010

11+
#deprecated
1112
let stderr : PipeWrite
1213

14+
#deprecated
1315
let stdin : PipeRead
1416

17+
#deprecated
1518
let stdout : PipeWrite
1619

1720
// Errors

src/process/moon.pkg.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"moonbitlang/async/internal/fd_util",
66
"moonbitlang/async/fs",
77
"moonbitlang/async/pipe",
8+
"moonbitlang/async/stdio",
89
"moonbitlang/async",
910
"moonbitlang/async/io",
1011
"moonbitlang/async/os_error"

src/process/pkg.generated.mbti

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package "moonbitlang/async/process"
44
import(
55
"moonbitlang/async/io"
66
"moonbitlang/async/pipe"
7+
"moonbitlang/async/stdio"
78
)
89

910
// Values
@@ -38,7 +39,9 @@ fn write_to_process() -> (&ProcessInput, @pipe.PipeWrite) raise
3839
// Traits
3940
trait ProcessInput
4041
impl ProcessInput for @pipe.PipeRead
42+
impl ProcessInput for @stdio.Input
4143

4244
trait ProcessOutput
4345
impl ProcessOutput for @pipe.PipeWrite
46+
impl ProcessOutput for @stdio.Output
4447

src/process/process.mbt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,11 @@ pub impl ProcessInput for @pipe.PipeRead with fd(self) {
415415
self.fd()
416416
}
417417

418+
///|
419+
pub impl ProcessInput for @stdio.Input with fd(self) {
420+
self.fd()
421+
}
422+
418423
///|
419424
/// An entity that can be used to redirect stdout/stderr of a process
420425
trait ProcessOutput {
@@ -432,6 +437,11 @@ pub impl ProcessOutput for @pipe.PipeWrite with fd(self) {
432437
self.fd()
433438
}
434439

440+
///|
441+
pub impl ProcessOutput for @stdio.Output with fd(self) {
442+
self.fd()
443+
}
444+
435445
///|
436446
priv struct TempPipeRead {
437447
pipe : @pipe.PipeRead

0 commit comments

Comments
 (0)