Skip to content

Commit b8e4354

Browse files
sunfishcodekubkon
authored andcommitted
Implement write_vectored for SandboxedTTYWriter.
Fixes #629.
1 parent 2a50701 commit b8e4354

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

crates/wasi-common/src/sandboxed_tty_writer.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::io::{Result, Write};
1+
use std::io::{IoSlice, Result, Write};
22

33
/// An adapter around a `Write` stream that guarantees that its output
44
/// is valid UTF-8 and contains no control characters. It does this by
@@ -124,6 +124,27 @@ where
124124
return Ok(result);
125125
}
126126

127+
fn write_vectored(&mut self, bufs: &[IoSlice]) -> Result<usize> {
128+
// Terminal output is [not expected to be atomic], so just write all the
129+
// individual buffers in sequence.
130+
//
131+
// [not expected to be atomic]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/read.html#tag_16_474_08
132+
let mut total_written = 0;
133+
134+
for buf in bufs {
135+
let written = self.write(buf)?;
136+
137+
total_written += written;
138+
139+
// Stop at the first point where the OS writes less than we asked.
140+
if written < buf.len() {
141+
break;
142+
}
143+
}
144+
145+
Ok(total_written)
146+
}
147+
127148
fn flush(&mut self) -> Result<()> {
128149
self.inner.flush()
129150
}

0 commit comments

Comments
 (0)