Skip to main content

web_wt_sys/streams/
readable_stream_read_result.rs

1//! [`ReadableStreamReadResult`]
2
3use wasm_bindgen::prelude::*;
4
5crate::dictionary_type! {
6    /// ```webidl
7    /// dictionary ReadableStreamReadResult {
8    ///   any value;
9    ///   boolean done;
10    /// };
11    /// ```
12    ///
13    /// <https://streams.spec.whatwg.org/#dictdef-readablestreamreadresult>
14    ///
15    /// The `T` type parameter is the type of the `value` field, matching
16    /// the chunk type of the stream the read result was produced by.
17    pub type ReadableStreamReadResult<T = JsValue> {
18        value: T => value
19        done: bool => done
20    }
21}
22
23impl<T> From<web_sys::ReadableStreamReadResult> for ReadableStreamReadResult<T>
24where
25    Self: JsCast,
26{
27    fn from(value: web_sys::ReadableStreamReadResult) -> Self {
28        value.unchecked_into()
29    }
30}
31
32impl<T> From<ReadableStreamReadResult<T>> for web_sys::ReadableStreamReadResult
33where
34    ReadableStreamReadResult<T>: JsCast,
35{
36    fn from(value: ReadableStreamReadResult<T>) -> Self {
37        value.unchecked_into()
38    }
39}
40
41impl<T> ReadableStreamReadResult<T>
42where
43    Self: JsCast,
44{
45    /// Returns `true` if `done` field exists and set to `true`.
46    pub fn is_done(&self) -> bool {
47        self.get_done().unwrap_or(false)
48    }
49}