1#[cfg(feature = "alloc")]
2use alloc::{vec, vec::Vec};
3
4use crate::io::ReadSimple;
5
6use super::Decoder;
7
8pub struct DecoderBuilder<R = (), B = ()> {
9 reader: R,
10 buf: B,
11}
12
13impl Default for DecoderBuilder {
14 fn default() -> Self {
15 Self {
16 reader: (),
17 buf: (),
18 }
19 }
20}
21
22impl DecoderBuilder<(), ()> {
23 #[deprecated = "DecoderBuilder is missing a reader and a buffer, which are required to build a decoder."]
24 pub fn build(self) -> ! {
25 panic!("See compilation warning");
26 }
27}
28
29impl<R> DecoderBuilder<(R,), ()> {
30 #[deprecated = "DecoderBuilder is missing a buffer, which is required to build a decoder."]
31 pub fn build(self) -> ! {
32 panic!("See compilation warning");
33 }
34}
35
36impl<B> DecoderBuilder<(), (B,)> {
37 #[deprecated = "DecoderBuilder is missing a reader, which is required to build a decoder."]
38 pub fn build(self) -> ! {
39 panic!("See compilation warning");
40 }
41}
42
43impl<B> DecoderBuilder<(), B> {
44 pub fn with_reader<R>(self, reader: R) -> DecoderBuilder<(R,), B>
45 where
46 R: ReadSimple,
47 {
48 DecoderBuilder {
49 reader: (reader,),
50 buf: self.buf,
51 }
52 }
53}
54
55impl<R> DecoderBuilder<R, ()> {
56 pub fn with_buffer<B>(self, buf: B) -> DecoderBuilder<R, (B,)>
57 where
58 B: AsMut<[u8]>,
59 {
60 DecoderBuilder {
61 reader: self.reader,
62 buf: (buf,),
63 }
64 }
65
66 pub fn with_stack_buffer<const SIZE: usize>(self) -> DecoderBuilder<R, ([u8; SIZE],)> {
67 self.with_buffer([0; SIZE])
68 }
69
70 #[cfg(feature = "alloc")]
71 pub fn with_heap_buffer(self, size: usize) -> DecoderBuilder<R, (Vec<u8>,)> {
72 self.with_buffer(vec![0; size])
73 }
74}
75
76impl<R, B> DecoderBuilder<(R,), (B,)>
77where
78 R: ReadSimple,
79 B: AsMut<[u8]>,
80{
81 pub fn build(self) -> Decoder<R, B> {
82 Decoder::new(self.reader.0, self.buf.0)
83 }
84}