base64_ng/engine/stream.rs
1#[cfg(feature = "stream")]
2use crate::stream;
3use crate::{Alphabet, Engine};
4
5impl<A, const PAD: bool> Engine<A, PAD>
6where
7 A: Alphabet,
8{
9 /// Wraps a `std::io::Write` value in a streaming Base64 encoder.
10 ///
11 /// This is a convenience constructor for [`stream::Encoder::new`] that
12 /// keeps the selected engine attached to the call site.
13 ///
14 /// ```
15 /// use std::io::Write;
16 /// use base64_ng::STANDARD;
17 ///
18 /// let mut encoder = STANDARD.encoder_writer(Vec::new());
19 /// encoder.write_all(b"hello").unwrap();
20 /// assert_eq!(encoder.finish().unwrap(), b"aGVsbG8=");
21 /// ```
22 #[cfg(feature = "stream")]
23 #[must_use]
24 pub fn encoder_writer<W>(&self, inner: W) -> stream::Encoder<W, A, PAD> {
25 stream::Encoder::new(inner, *self)
26 }
27
28 /// Wraps a `std::io::Write` value in a streaming Base64 decoder.
29 ///
30 /// This is a convenience constructor for [`stream::Decoder::new`] that
31 /// keeps the selected engine attached to the call site.
32 ///
33 /// ```
34 /// use std::io::Write;
35 /// use base64_ng::STANDARD;
36 ///
37 /// let mut decoder = STANDARD.decoder_writer(Vec::new());
38 /// decoder.write_all(b"aGVsbG8=").unwrap();
39 /// assert_eq!(decoder.finish().unwrap(), b"hello");
40 /// ```
41 ///
42 /// # Security
43 ///
44 /// Streaming decoders use the normal strict decode path, not the
45 /// [`crate::ct`] module. Do not use this adapter for secret-bearing
46 /// payloads when malformed-input timing matters.
47 #[cfg(feature = "stream")]
48 #[must_use]
49 pub fn decoder_writer<W>(&self, inner: W) -> stream::Decoder<W, A, PAD> {
50 stream::Decoder::new(inner, *self)
51 }
52
53 /// Wraps a `std::io::Read` value in a streaming Base64 encoder.
54 ///
55 /// This is a convenience constructor for [`stream::EncoderReader::new`]
56 /// that keeps the selected engine attached to the call site.
57 ///
58 /// ```
59 /// use std::io::Read;
60 /// use base64_ng::STANDARD;
61 ///
62 /// let mut reader = STANDARD.encoder_reader(&b"hello"[..]);
63 /// let mut encoded = String::new();
64 /// reader.read_to_string(&mut encoded).unwrap();
65 /// assert_eq!(encoded, "aGVsbG8=");
66 /// ```
67 #[cfg(feature = "stream")]
68 #[must_use]
69 pub fn encoder_reader<R>(&self, inner: R) -> stream::EncoderReader<R, A, PAD> {
70 stream::EncoderReader::new(inner, *self)
71 }
72
73 /// Wraps a `std::io::Read` value in a streaming Base64 decoder.
74 ///
75 /// This is a convenience constructor for [`stream::DecoderReader::new`]
76 /// that keeps the selected engine attached to the call site.
77 ///
78 /// ```
79 /// use std::io::Read;
80 /// use base64_ng::STANDARD;
81 ///
82 /// let mut reader = STANDARD.decoder_reader(&b"aGVsbG8="[..]);
83 /// let mut decoded = Vec::new();
84 /// reader.read_to_end(&mut decoded).unwrap();
85 /// assert_eq!(decoded, b"hello");
86 /// ```
87 ///
88 /// # Security
89 ///
90 /// Streaming decoder readers use the normal strict decode path, not the
91 /// [`crate::ct`] module. Do not use this adapter for secret-bearing
92 /// payloads when malformed-input timing matters.
93 #[cfg(feature = "stream")]
94 #[must_use]
95 pub fn decoder_reader<R>(&self, inner: R) -> stream::DecoderReader<R, A, PAD> {
96 stream::DecoderReader::new(inner, *self)
97 }
98}