Types
use resource output-streamsince 0.2.0
An output bytestream.
output-streams are non-blocking to the extent practical on
underlying platforms. Except where specified otherwise, I/O operations also
always return promptly, after the number of bytes that can be written
promptly, which could even be zero. To wait for the stream to be ready to
accept data, the subscribe function to obtain a pollable which can be
polled for using wasi:io/poll.
Dropping an output-stream while there's still an active write in
progress may result in the data being lost. Before dropping the stream,
be sure to fully flush your writes.
check-write: func() -> result<u64, stream-error>since 0.2.0Check readiness for writing. This function never blocks.
Returns the number of bytes permitted for the next call to
write, or an error. Callingwritewith more bytes than this function has permitted will trap.When this function returns 0 bytes, the
subscribepollable will become ready when this function will report at least 1 byte, or an error.write: func(contents: list<u8>) -> result<_, stream-error>since 0.2.0Perform a write. This function never blocks.
When the destination of a
writeis binary data, the bytes fromcontentsare written verbatim. When the destination of awriteis known to the implementation to be text, the bytes ofcontentsare transcoded from UTF-8 into the encoding of the destination and then written.Precondition: check-write gave permit of Ok(n) and contents has a length of less than or equal to n. Otherwise, this function will trap.
returns Err(closed) without writing if the stream has closed since the last call to check-write provided a permit.
blocking-write-and-flush: func( contents: list<u8>, ) -> result<_, stream-error>since 0.2.0Perform a write of up to 4096 bytes, and then flush the stream. Block until all of these operations are complete, or an error occurs.
This is a convenience wrapper around the use of
check-write,subscribe,write, andflush, and is implemented with the following pseudo-code:let pollable = this.subscribe(); while !contents.is_empty() { // Wait for the stream to become writable pollable.block(); let Ok(n) = this.check-write(); // eliding error handling let len = min(n, contents.len()); let (chunk, rest) = contents.split_at(len); this.write(chunk ); // eliding error handling contents = rest; } this.flush(); // Wait for completion of `flush` pollable.block(); // Check for any errors that arose during `flush` let _ = this.check-write(); // eliding error handlingflush: func() -> result<_, stream-error>since 0.2.0Request to flush buffered output. This function never blocks.
This tells the output-stream that the caller intends any buffered output to be flushed. the output which is expected to be flushed is all that has been passed to
writeprior to this call.Upon calling this function, the
output-streamwill not accept any writes (check-writewill returnok(0)) until the flush has completed. Thesubscribepollable will become ready when the flush has completed and the stream can accept more writes.blocking-flush: func() -> result<_, stream-error>since 0.2.0Request to flush buffered output, and block until flush completes and stream is ready for writing again.
subscribe: func() -> pollablesince 0.2.0Create a
pollablewhich will resolve once the output-stream is ready for more writing, or an error has occurred. When this pollable is ready,check-writewill returnok(n)with n>0, or an error.If the stream is closed, this pollable is always ready immediately.
The created
pollableis a child resource of theoutput-stream. Implementations may trap if theoutput-streamis dropped before all derivedpollables created with this function are dropped.write-zeroes: func(len: u64) -> result<_, stream-error>since 0.2.0Write zeroes to a stream.
This should be used precisely like
writewith the exact same preconditions (must use check-write first), but instead of passing a list of bytes, you simply pass the number of zero-bytes that should be written.blocking-write-zeroes-and-flush: func( len: u64, ) -> result<_, stream-error>since 0.2.0Perform a write of up to 4096 zeroes, and then flush the stream. Block until all of these operations are complete, or an error occurs.
This is a convenience wrapper around the use of
check-write,subscribe,write-zeroes, andflush, and is implemented with the following pseudo-code:let pollable = this.subscribe(); while num_zeroes != 0 { // Wait for the stream to become writable pollable.block(); let Ok(n) = this.check-write(); // eliding error handling let len = min(n, num_zeroes); this.write-zeroes(len); // eliding error handling num_zeroes -= len; } this.flush(); // Wait for completion of `flush` pollable.block(); // Check for any errors that arose during `flush` let _ = this.check-write(); // eliding error handlingsplice: func( src: borrow<input-stream>, len: u64, ) -> result<u64, stream-error>since 0.2.0Read from one stream and write to another.
The behavior of splice is equivalent to:
- calling
check-writeon theoutput-stream - calling
readon theinput-streamwith the smaller of thecheck-writepermitted length and thelenprovided tosplice - calling
writeon theoutput-streamwith that read data.
Any error reported by the call to
check-write,read, orwriteends the splice and reports that error.This function returns the number of bytes transferred; it may be less than
len.- calling
blocking-splice: func( src: borrow<input-stream>, len: u64, ) -> result<u64, stream-error>since 0.2.0Read from one stream and write to another, with blocking.
This is similar to
splice, except that it blocks until theoutput-streamis ready for writing, and theinput-streamis ready for reading, before performing thesplice.
Functions
get-stderr: func() -> output-streamsince 0.2.0
WIT
@since(version = 0.2.0)
interface stderr {
@since(version = 0.2.0)
use wasi:io/streams@0.2.6.{output-stream};
@since(version = 0.2.0)
get-stderr: func() -> output-stream;
}