@@ -5,7 +5,9 @@ use magnus::{
5
5
} ;
6
6
use std:: io:: Write ;
7
7
use std:: sync:: { Arc , Mutex } ;
8
- use wasmtime_wasi:: p2:: { OutputStream , Pollable , StdoutStream , StreamError , StreamResult } ;
8
+ use tokio:: io:: AsyncWrite ;
9
+ use wasmtime_wasi:: cli:: { IsTerminal , StdoutStream } ;
10
+ use wasmtime_wasi:: p2:: { OutputStream , Pollable , StreamError , StreamResult } ;
9
11
10
12
/// A buffer that limits the number of bytes that can be written to it.
11
13
/// If the buffer is full, it will truncate the data.
@@ -14,6 +16,31 @@ pub struct OutputLimitedBuffer {
14
16
inner : Arc < Mutex < OutputLimitedBufferInner > > ,
15
17
}
16
18
19
+ // No support for WASI P3, yet.
20
+ impl AsyncWrite for OutputLimitedBuffer {
21
+ fn poll_write (
22
+ self : std:: pin:: Pin < & mut Self > ,
23
+ _cx : & mut std:: task:: Context < ' _ > ,
24
+ _buf : & [ u8 ] ,
25
+ ) -> std:: task:: Poll < Result < usize , std:: io:: Error > > {
26
+ std:: task:: Poll :: Ready ( Ok ( 0 ) )
27
+ }
28
+
29
+ fn poll_flush (
30
+ self : std:: pin:: Pin < & mut Self > ,
31
+ _cx : & mut std:: task:: Context < ' _ > ,
32
+ ) -> std:: task:: Poll < Result < ( ) , std:: io:: Error > > {
33
+ std:: task:: Poll :: Ready ( Ok ( ( ) ) )
34
+ }
35
+
36
+ fn poll_shutdown (
37
+ self : std:: pin:: Pin < & mut Self > ,
38
+ _cx : & mut std:: task:: Context < ' _ > ,
39
+ ) -> std:: task:: Poll < Result < ( ) , std:: io:: Error > > {
40
+ std:: task:: Poll :: Ready ( Ok ( ( ) ) )
41
+ }
42
+ }
43
+
17
44
impl OutputLimitedBuffer {
18
45
/// Creates a new [OutputLimitedBuffer] with the given underlying buffer
19
46
/// and capacity.
@@ -33,12 +60,19 @@ impl Clone for OutputLimitedBuffer {
33
60
}
34
61
35
62
impl StdoutStream for OutputLimitedBuffer {
36
- fn stream ( & self ) -> Box < dyn OutputStream > {
63
+ fn p2_stream ( & self ) -> Box < dyn OutputStream > {
64
+ let cloned = self . clone ( ) ;
65
+ Box :: new ( cloned)
66
+ }
67
+
68
+ fn async_stream ( & self ) -> Box < dyn AsyncWrite + Send + Sync > {
37
69
let cloned = self . clone ( ) ;
38
70
Box :: new ( cloned)
39
71
}
72
+ }
40
73
41
- fn isatty ( & self ) -> bool {
74
+ impl IsTerminal for OutputLimitedBuffer {
75
+ fn is_terminal ( & self ) -> bool {
42
76
false
43
77
}
44
78
}
0 commit comments