diff --git a/src/readable/byob_reader.rs b/src/readable/byob_reader.rs index 36c473f..4f90e40 100644 --- a/src/readable/byob_reader.rs +++ b/src/readable/byob_reader.rs @@ -117,15 +117,15 @@ impl<'stream> ReadableStreamBYOBReader<'stream> { let view = buffer.subarray(0, dst_len).unchecked_into::(); // Read into view. This transfers `buffer.buffer()`. let promise = self.as_raw().read_with_array_buffer_view(&view); - let js_value = JsFuture::from(promise).await?; - let result = sys::ReadableStreamBYOBReadResult::from(js_value); - let filled_view = match result.value() { - Some(view) => view, - None => { - // No new view was returned. The stream must have been canceled. - assert!(result.is_done()); - return Ok((0, None)); - } + let js_result = JsFuture::from(promise).await?; + let result = sys::ReadableStreamReadResult::from(js_result); + let js_value = result.value(); + let filled_view = if js_value.is_undefined() { + // No new view was returned. The stream must have been canceled. + assert!(result.is_done()); + return Ok((0, None)); + } else { + js_value.unchecked_into::() }; let filled_len = checked_cast_to_usize(filled_view.byte_length()); debug_assert!(filled_len <= dst.len()); diff --git a/src/readable/default_reader.rs b/src/readable/default_reader.rs index 21ab949..15be8f3 100644 --- a/src/readable/default_reader.rs +++ b/src/readable/default_reader.rs @@ -70,8 +70,8 @@ impl<'stream> ReadableStreamDefaultReader<'stream> { /// * If the stream encounters an `error`, this returns `Err(error)`. pub async fn read(&mut self) -> Result, JsValue> { let promise = self.as_raw().read(); - let js_value = JsFuture::from(promise).await?; - let result = sys::ReadableStreamDefaultReadResult::from(js_value); + let js_result = JsFuture::from(promise).await?; + let result = sys::ReadableStreamReadResult::from(js_result); if result.is_done() { Ok(None) } else { diff --git a/src/readable/into_async_read.rs b/src/readable/into_async_read.rs index a400cb0..cc6d238 100644 --- a/src/readable/into_async_read.rs +++ b/src/readable/into_async_read.rs @@ -11,7 +11,7 @@ use wasm_bindgen_futures::JsFuture; use crate::util::{checked_cast_to_usize, clamp_to_u32, js_to_io_error}; -use super::sys::ReadableStreamBYOBReadResult; +use super::sys::ReadableStreamReadResult; use super::ReadableStreamBYOBReader; /// An [`AsyncRead`] for the [`into_async_read`](super::ReadableStream::into_async_read) method. @@ -107,14 +107,14 @@ impl<'reader> AsyncRead for IntoAsyncRead<'reader> { // Read completed Poll::Ready(match js_result { Ok(js_value) => { - let result = ReadableStreamBYOBReadResult::from(js_value); + let result = ReadableStreamReadResult::from(js_value); if result.is_done() { // End of stream self.discard_reader(); Ok(0) } else { // Cannot be canceled, so view must exist - let filled_view = result.value().unwrap_throw(); + let filled_view = result.value().unchecked_into::(); // Copy bytes to output buffer let filled_len = checked_cast_to_usize(filled_view.byte_length()); debug_assert!(filled_len <= buf.len()); diff --git a/src/readable/into_stream.rs b/src/readable/into_stream.rs index 769decf..92f57d2 100644 --- a/src/readable/into_stream.rs +++ b/src/readable/into_stream.rs @@ -7,7 +7,7 @@ use futures_util::FutureExt; use wasm_bindgen::prelude::*; use wasm_bindgen_futures::JsFuture; -use super::sys::ReadableStreamDefaultReadResult; +use super::sys::ReadableStreamReadResult; use super::ReadableStreamDefaultReader; /// A [`Stream`] for the [`into_stream`](super::ReadableStream::into_stream) method. @@ -87,7 +87,7 @@ impl<'reader> Stream for IntoStream<'reader> { // Read completed Poll::Ready(match js_result { Ok(js_value) => { - let result = ReadableStreamDefaultReadResult::from(js_value); + let result = ReadableStreamReadResult::from(js_value); if result.is_done() { // End of stream, drop reader self.reader = None; diff --git a/src/readable/sys.rs b/src/readable/sys.rs index 2aebbf8..fe6104f 100644 --- a/src/readable/sys.rs +++ b/src/readable/sys.rs @@ -1,7 +1,7 @@ //! Raw bindings to JavaScript objects used //! by a [`ReadableStream`](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream). //! These are re-exported from [web-sys](https://docs.rs/web-sys/0.3.64/web_sys/struct.ReadableStream.html). -use js_sys::{Array, Error, Object, Uint8Array}; +use js_sys::{Array, Error, Object}; use wasm_bindgen::prelude::*; use web_sys::QueuingStrategy; pub use web_sys::ReadableByteStreamController; @@ -60,26 +60,14 @@ extern "C" { #[wasm_bindgen] extern "C" { - /// A result returned by [`ReadableStreamDefaultReader.read`](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStreamDefaultReader/read). + /// A result returned by [`ReadableStreamDefaultReader.read`](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStreamDefaultReader/read) + /// or [`ReadableStreamBYOBReader.read`](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStreamBYOBReader/read). #[derive(Clone, Debug)] - pub(crate) type ReadableStreamDefaultReadResult; + pub(crate) type ReadableStreamReadResult; #[wasm_bindgen(method, getter, js_name = done)] - pub(crate) fn is_done(this: &ReadableStreamDefaultReadResult) -> bool; + pub(crate) fn is_done(this: &ReadableStreamReadResult) -> bool; #[wasm_bindgen(method, getter, js_name = value)] - pub(crate) fn value(this: &ReadableStreamDefaultReadResult) -> JsValue; -} - -#[wasm_bindgen] -extern "C" { - /// A result returned by [`ReadableStreamBYOBReader.read`](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStreamBYOBReader/read). - #[derive(Clone, Debug)] - pub(crate) type ReadableStreamBYOBReadResult; - - #[wasm_bindgen(method, getter, js_name = done)] - pub(crate) fn is_done(this: &ReadableStreamBYOBReadResult) -> bool; - - #[wasm_bindgen(method, getter, js_name = value)] - pub(crate) fn value(this: &ReadableStreamBYOBReadResult) -> Option; + pub(crate) fn value(this: &ReadableStreamReadResult) -> JsValue; }