wasm_streams/transform/mod.rs
1//! Bindings and conversions for
2//! [transform streams](https://developer.mozilla.org/en-US/docs/Web/API/TransformStream).
3use crate::readable::ReadableStream;
4use crate::writable::WritableStream;
5
6pub mod sys;
7
8/// A [`TransformStream`](https://developer.mozilla.org/en-US/docs/Web/API/TransformStream).
9///
10/// `TransformStream`s can be created from a [raw JavaScript stream](sys::TransformStream) with
11/// [`from_raw`](Self::from_raw), and can be converted back with [`into_raw`](Self::into_raw).
12///
13/// Use [`readable`](Self::readable) and [`writable`](Self::writable) to access the readable and
14/// writable side of the transform stream.
15/// These can then be converted into a Rust [`Stream`] and [`Sink`] respectively
16/// using [`into_stream`](super::ReadableStream::into_stream)
17/// and [`into_sink`](super::WritableStream::into_sink).
18///
19/// [`Stream`]: https://docs.rs/futures/0.3.30/futures/stream/trait.Stream.html
20/// [`Sink`]: https://docs.rs/futures/0.3.30/futures/sink/trait.Sink.html
21#[derive(Debug)]
22pub struct TransformStream {
23 raw: sys::TransformStream,
24}
25
26impl TransformStream {
27 /// Creates a new `TransformStream` from a [JavaScript stream](sys::TransformStream).
28 #[inline]
29 pub fn from_raw(raw: sys::TransformStream) -> Self {
30 Self { raw }
31 }
32
33 /// Acquires a reference to the underlying [JavaScript stream](sys::TransformStream).
34 #[inline]
35 pub fn as_raw(&self) -> &sys::TransformStream {
36 &self.raw
37 }
38
39 /// Consumes this `TransformStream`, returning the underlying [JavaScript stream](sys::TransformStream).
40 #[inline]
41 pub fn into_raw(self) -> sys::TransformStream {
42 self.raw
43 }
44
45 /// Returns the readable side of the transform stream.
46 #[inline]
47 pub fn readable(&self) -> ReadableStream {
48 ReadableStream::from_raw(self.as_raw().readable())
49 }
50
51 /// Returns the writable side of the transform stream.
52 #[inline]
53 pub fn writable(&self) -> WritableStream {
54 WritableStream::from_raw(self.as_raw().writable())
55 }
56}