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