wasm_streams/readable/
pipe_options.rs

1use web_sys::AbortSignal;
2
3use super::sys;
4
5/// Options for [`pipe_to_with_options`](super::ReadableStream::pipe_to_with_options).
6#[derive(Clone, Debug, Default)]
7pub struct PipeOptions {
8    raw: sys::PipeOptions,
9}
10
11impl PipeOptions {
12    /// Creates a blank new set of pipe options.
13    ///
14    /// Equivalent to [`PipeOptions::default`](Default::default).
15    pub fn new() -> Self {
16        Default::default()
17    }
18
19    /// Creates a set of pipe options from a raw [`PipeOptions`](sys::PipeOptions) object.
20    #[inline]
21    pub fn from_raw(raw: sys::PipeOptions) -> Self {
22        Self { raw }
23    }
24
25    /// Convert this to a raw [`PipeOptions`](sys::PipeOptions) object.
26    #[inline]
27    pub fn into_raw(self) -> sys::PipeOptions {
28        self.raw
29    }
30
31    /// Sets whether the destination writable stream should be closed
32    /// when the source readable stream closes.
33    pub fn prevent_close(&mut self, prevent_close: bool) -> &mut Self {
34        self.raw.set_prevent_close(prevent_close);
35        self
36    }
37
38    /// Sets whether the source readable stream should be [canceled](https://streams.spec.whatwg.org/#cancel-a-readable-stream)
39    /// when the destination writable stream errors.
40    pub fn prevent_cancel(&mut self, prevent_cancel: bool) -> &mut Self {
41        self.raw.set_prevent_cancel(prevent_cancel);
42        self
43    }
44
45    /// Sets whether the destination writable stream should be [aborted](https://streams.spec.whatwg.org/#abort-a-writable-stream)
46    /// when the source readable stream errors.
47    pub fn prevent_abort(&mut self, prevent_abort: bool) -> &mut Self {
48        self.raw.set_prevent_abort(prevent_abort);
49        self
50    }
51
52    /// Sets an abort signal to abort the ongoing pipe operation.
53    /// When the signal is aborted, the source readable stream will be canceled
54    /// and the destination writable stream will be aborted
55    /// unless the respective options [`prevent_cancel`](Self::prevent_cancel)
56    /// or [`prevent_abort`](Self::prevent_abort) are set.
57    pub fn signal(&mut self, signal: AbortSignal) -> &mut Self {
58        self.raw.set_signal(&signal);
59        self
60    }
61}