Struct wasm_streams::readable::ReadableStreamBYOBReader [−][src]
pub struct ReadableStreamBYOBReader<'stream> { /* fields omitted */ }Expand description
A ReadableStreamBYOBReader
that can be used to read chunks from a ReadableStream.
This is returned by the get_byob_reader method.
When the reader is dropped, it automatically releases its lock.
If the reader still has a pending read request at this point (i.e. if a future returned
by read is not yet ready), then this will panic. You must either await
all read futures, or cancel the stream to discard any pending read futures.
Implementations
Acquires a reference to the underlying JavaScript reader.
Waits for the stream to become closed.
This returns an error if the stream ever errors, or if the reader’s lock is released before the stream finishes closing.
Cancels the stream, signaling a loss of interest in the stream by a consumer.
Equivalent to ReadableStream.cancel.
Cancels the stream, signaling a loss of interest in the stream by a consumer.
Equivalent to ReadableStream.cancel_with_reason.
Reads the next chunk from the stream’s internal queue into dst,
and returns the number of bytes read.
- If some bytes were read into
dst, this returnsOk(bytes_read). - If the stream closes and no more bytes are available, this returns
Ok(0). - If the stream cancels, this returns
Ok(0). - If the stream encounters an
error, this returnsErr(error).
This always allocated a new temporary Uint8Array with the same size as dst to hold
the result before copying to dst. We cannot pass a view on the backing WebAssembly memory
directly, because:
reader.read(view)needs to transferview.buffer, butWebAssembly.Memorybuffers are non-transferable.view.buffercan be invalidated if the WebAssembly memory grows whileread(view)is still in progress.
Therefore, it is necessary to use a separate buffer living in the JavaScript heap.
To avoid repeated allocations for repeated reads,
use read_with_buffer.
pub async fn read_with_buffer(
&mut self,
dst: &mut [u8],
buffer: Uint8Array
) -> Result<(usize, Option<Uint8Array>), JsValue>
pub async fn read_with_buffer(
&mut self,
dst: &mut [u8],
buffer: Uint8Array
) -> Result<(usize, Option<Uint8Array>), JsValue>
Reads the next chunk from the stream’s internal queue into dst,
and returns the number of bytes read.
The given buffer is used to store the bytes before they are copied to dst.
This buffer is returned back together with the result, so it can be re-used for subsequent
reads without extra allocations. Note that the underlying ArrayBuffer is transferred
in the process, so any other views on the original buffer will become unusable.
- If some bytes were read into
dst, this returnsOk((bytes_read, Some(buffer))). - If the stream closes and no more bytes are available, this returns
Ok((0, Some(buffer))). - If the stream cancels, this returns
Ok((0, None)). In this case, the given buffer is not returned. - If the stream encounters an
error, this returnsErr(error).
Releases this reader’s lock on the corresponding stream.
Panics if the reader still has a pending read request, i.e. if a future returned
by read is not yet ready. For a non-panicking variant,
use try_release_lock.
Converts this ReadableStreamBYOBReader into an AsyncRead.
This is similar to ReadableStream.into_async_read,
except that after the returned AsyncRead is dropped, the original ReadableStream is
still usable. This allows reading only a few bytes from the AsyncRead, while still
allowing another reader to read the remaining bytes later on.