Debian Codegraph @

src/stream/zio/reader.rs

raw symbols SCIP
1 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::{selfrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/, BufReadrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/BufRead#, Readrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#};
2
3 use craterust-analyzer cargo zstd 0.13.3 crate/::streamrust-analyzer cargo zstd 0.13.3 stream/::rawrust-analyzer cargo zstd 0.13.3 stream/raw/::{InBufferrust-analyzer cargo zstd-safe 7.2.2 InBuffer#, Operationrust-analyzer cargo zstd 0.13.3 stream/raw/Operation#, OutBufferrust-analyzer cargo zstd-safe 7.2.2 OutBuffer#};
4
5 // [ reader -> zstd ] -> output
6 /// Implements the [`Read`] API around an [`Operation`].
7 ///
8 /// This can be used to wrap a raw in-memory operation in a read-focused API.
9 ///
10 /// It can wrap either a compression or decompression operation, and pulls
11 /// input data from a wrapped `Read`.
12 pub struct Readerrust-analyzer cargo zstd 0.13.3 stream/zio/reader/Reader#<R, D> {
13 readerrust-analyzer cargo zstd 0.13.3 stream/zio/reader/Reader#reader.: R,
14 operationrust-analyzer cargo zstd 0.13.3 stream/zio/reader/Reader#operation.: D,
15
16 staterust-analyzer cargo zstd 0.13.3 stream/zio/reader/Reader#state.: Staterust-analyzer cargo zstd 0.13.3 stream/zio/reader/State#,
17
18 single_framerust-analyzer cargo zstd 0.13.3 stream/zio/reader/Reader#single_frame.: bool,
19 finished_framerust-analyzer cargo zstd 0.13.3 stream/zio/reader/Reader#finished_frame.: bool,
20 }
21
22 enum Staterust-analyzer cargo zstd 0.13.3 stream/zio/reader/State# {
23 // Still actively reading from the inner `Read`
24 Readingrust-analyzer cargo zstd 0.13.3 stream/zio/reader/State#Reading#,
25 // We reached EOF from the inner `Read`, now flushing.
26 PastEofrust-analyzer cargo zstd 0.13.3 stream/zio/reader/State#PastEof#,
27 // We are fully done, nothing can be read.
28 Finishedrust-analyzer cargo zstd 0.13.3 stream/zio/reader/State#Finished#,
29 }
30
31 impl<R, D> Readerrust-analyzer cargo zstd 0.13.3 stream/zio/reader/Reader#<R, D> {
32 /// Creates a new `Reader`.
33 ///
34 /// `reader` will be used to pull input data for the given operation.
35 pub fn newrust-analyzer cargo zstd 0.13.3 stream/zio/reader/impl#[`Reader<R, D>`]new().(reader: R, operation: D) -> Selfrust-analyzer cargo zstd 0.13.3 stream/zio/reader/impl#[`Reader<R, D>`] {
36 Readerrust-analyzer cargo zstd 0.13.3 stream/zio/reader/Reader# {
37 readerrust-analyzer cargo zstd 0.13.3 stream/zio/reader/Reader#reader.,
38 operationrust-analyzer cargo zstd 0.13.3 stream/zio/reader/Reader#operation.,
39 staterust-analyzer cargo zstd 0.13.3 stream/zio/reader/Reader#state.: Staterust-analyzer cargo zstd 0.13.3 stream/zio/reader/State#::Readingrust-analyzer cargo zstd 0.13.3 stream/zio/reader/State#Reading#,
40 single_framerust-analyzer cargo zstd 0.13.3 stream/zio/reader/Reader#single_frame.: false,
41 finished_framerust-analyzer cargo zstd 0.13.3 stream/zio/reader/Reader#finished_frame.: false,
42 }
43 }
44
45 /// Sets `self` to stop after the first decoded frame.
46 pub fn set_single_framerust-analyzer cargo zstd 0.13.3 stream/zio/reader/impl#[`Reader<R, D>`]set_single_frame().(&mut self) {
47 self.single_framerust-analyzer cargo zstd 0.13.3 stream/zio/reader/Reader#single_frame. = true;
48 }
49
50 /// Returns a mutable reference to the underlying operation.
51 pub fn operation_mutrust-analyzer cargo zstd 0.13.3 stream/zio/reader/impl#[`Reader<R, D>`]operation_mut().(&mut self) -> &mut D {
52 &mut self.operationrust-analyzer cargo zstd 0.13.3 stream/zio/reader/Reader#operation.
53 }
54
55 /// Returns a mutable reference to the underlying reader.
56 pub fn reader_mutrust-analyzer cargo zstd 0.13.3 stream/zio/reader/impl#[`Reader<R, D>`]reader_mut().(&mut self) -> &mut R {
57 &mut self.readerrust-analyzer cargo zstd 0.13.3 stream/zio/reader/Reader#reader.
58 }
59
60 /// Returns a reference to the underlying reader.
61 pub fn readerrust-analyzer cargo zstd 0.13.3 stream/zio/reader/impl#[`Reader<R, D>`]reader().(&self) -> &R {
62 &self.readerrust-analyzer cargo zstd 0.13.3 stream/zio/reader/Reader#reader.
63 }
64
65 /// Returns the inner reader.
66 pub fn into_innerrust-analyzer cargo zstd 0.13.3 stream/zio/reader/impl#[`Reader<R, D>`]into_inner().(self) -> R {
67 self.readerrust-analyzer cargo zstd 0.13.3 stream/zio/reader/Reader#reader.
68 }
69
70 /// Flush any internal buffer.
71 ///
72 /// For encoders, this ensures all input consumed so far is compressed.
73 pub fn flushrust-analyzer cargo zstd 0.13.3 stream/zio/reader/impl#[`Reader<R, D>`]flush().(&mut self, output: &mut [u8]) -> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<usize>
74 where
75 D: Operationrust-analyzer cargo zstd 0.13.3 stream/raw/Operation#,
76 {
77 self.operationrust-analyzer cargo zstd 0.13.3 stream/zio/reader/Reader#operation..flushrust-analyzer cargo zstd 0.13.3 stream/raw/Operation#flush().(&mut OutBufferrust-analyzer cargo zstd-safe 7.2.2 OutBuffer#::aroundrust-analyzer cargo zstd-safe 7.2.2 impl#[`OutBuffer<'a, C>`]around().(output))
78 }
79 }
80 // Read and retry on Interrupted errors.
81 fn fill_bufrust-analyzer cargo zstd 0.13.3 stream/zio/reader/fill_buf().<R>(reader: &mut R) -> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<&[u8]>
82 where
83 R: BufReadrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/BufRead#,
84 {
85 // This doesn't work right now because of the borrow-checker.
86 // When it can be made to compile, it would allow Reader to automatically
87 // retry on `Interrupted` error.
88 /*
89 loop {
90 match reader.fill_buf() {
91 Err(ref e) if e.kind() == io::ErrorKind::Interrupted => {}
92 otherwise => return otherwise,
93 }
94 }
95 */
96
97 // Workaround for now
98 let res = reader.fill_bufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/BufRead#fill_buf().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
99
100 // eprintln!("Filled buffer: {:?}", res);
101
102 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(res)
103 }
104
105 impl<R, D> Readrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read# for Readerrust-analyzer cargo zstd 0.13.3 stream/zio/reader/Reader#<R, D>
106 where
107 R: BufReadrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/BufRead#,
108 D: Operationrust-analyzer cargo zstd 0.13.3 stream/raw/Operation#,
109 {
110 fn readrust-analyzer cargo zstd 0.13.3 stream/zio/reader/impl#[`Reader<R, D>`][Read]read().(&mut self, buf: &mut [u8]) -> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<usize> {
111 // Keep trying until _something_ has been written.
112 let mut first = true;
113 loop {
114 match self.staterust-analyzer cargo zstd 0.13.3 stream/zio/reader/Reader#state. {
115 Staterust-analyzer cargo zstd 0.13.3 stream/zio/reader/State#::Readingrust-analyzer cargo zstd 0.13.3 stream/zio/reader/State#Reading# => {
116 let (bytes_read, bytes_written) = {
117 // Start with a fresh pool of un-processed data.
118 // This is the only line that can return an interruption error.
119 let input = if first {
120 // eprintln!("First run, no input coming.");
121 b""
122 } else {
123 fill_bufrust-analyzer cargo zstd 0.13.3 stream/zio/reader/fill_buf().(&mut self.readerrust-analyzer cargo zstd 0.13.3 stream/zio/reader/Reader#reader.)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().
124 };
125
126 // eprintln!("Input = {:?}", input);
127
128 // It's possible we don't have any new data to read.
129 // (In this case we may still have zstd's own buffer to clear.)
130 if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().first && input.is_emptyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]is_empty().() {
131 self.staterust-analyzer cargo zstd 0.13.3 stream/zio/reader/Reader#state. = Staterust-analyzer cargo zstd 0.13.3 stream/zio/reader/State#::PastEofrust-analyzer cargo zstd 0.13.3 stream/zio/reader/State#PastEof#;
132 continue;
133 }
134 first = false;
135
136 let mut src = InBufferrust-analyzer cargo zstd-safe 7.2.2 InBuffer#::aroundrust-analyzer cargo zstd-safe 7.2.2 impl#[`InBuffer<'a>`]around().(input);
137 let mut dst = OutBufferrust-analyzer cargo zstd-safe 7.2.2 OutBuffer#::aroundrust-analyzer cargo zstd-safe 7.2.2 impl#[`OutBuffer<'a, C>`]around().(buf);
138
139 // We don't want empty input (from first=true) to cause a frame
140 // re-initialization.
141 if self.finished_framerust-analyzer cargo zstd 0.13.3 stream/zio/reader/Reader#finished_frame. && !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().input.is_emptyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]is_empty().() {
142 // eprintln!("!! Reigniting !!");
143 self.operationrust-analyzer cargo zstd 0.13.3 stream/zio/reader/Reader#operation..reinitrust-analyzer cargo zstd 0.13.3 stream/raw/Operation#reinit().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
144 self.finished_framerust-analyzer cargo zstd 0.13.3 stream/zio/reader/Reader#finished_frame. = false;
145 }
146
147 // Phase 1: feed input to the operation
148 let hint = self.operationrust-analyzer cargo zstd 0.13.3 stream/zio/reader/Reader#operation..runrust-analyzer cargo zstd 0.13.3 stream/raw/Operation#run().(&mut src, &mut dst)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
149 // eprintln!(
150 // "Hint={} Just run our operation:\n In={:?}\n Out={:?}",
151 // hint, src, dst
152 // );
153
154 if hint rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().0 {
155 // In practice this only happens when decoding, when we just finished
156 // reading a frame.
157 self.finished_framerust-analyzer cargo zstd 0.13.3 stream/zio/reader/Reader#finished_frame. = true;
158 if self.single_framerust-analyzer cargo zstd 0.13.3 stream/zio/reader/Reader#single_frame. {
159 self.staterust-analyzer cargo zstd 0.13.3 stream/zio/reader/Reader#state. = Staterust-analyzer cargo zstd 0.13.3 stream/zio/reader/State#::Finishedrust-analyzer cargo zstd 0.13.3 stream/zio/reader/State#Finished#;
160 }
161 }
162
163 // eprintln!("Output: {:?}", dst);
164
165 (src.posrust-analyzer cargo zstd-safe 7.2.2 impl#[`InBuffer<'a>`]pos().(), dst.posrust-analyzer cargo zstd-safe 7.2.2 impl#[`OutBuffer<'a, C>`]pos().())
166 };
167
168 self.readerrust-analyzer cargo zstd 0.13.3 stream/zio/reader/Reader#reader..consumerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/BufRead#consume().(bytes_read);
169
170 if bytes_written rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().0 {
171 return Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(bytes_written);
172 }
173
174 // We need more data! Try again!
175 }
176 Staterust-analyzer cargo zstd 0.13.3 stream/zio/reader/State#::PastEofrust-analyzer cargo zstd 0.13.3 stream/zio/reader/State#PastEof# => {
177 let mut dst = OutBufferrust-analyzer cargo zstd-safe 7.2.2 OutBuffer#::aroundrust-analyzer cargo zstd-safe 7.2.2 impl#[`OutBuffer<'a, C>`]around().(buf);
178
179 // We already sent all the input we could get to zstd. Time to flush out the
180 // buffer and be done with it.
181
182 // Phase 2: flush out the operation's buffer
183 // Keep calling `finish()` until the buffer is empty.
184 let hint = self
185 .operationrust-analyzer cargo zstd 0.13.3 stream/zio/reader/Reader#operation.
186 .finishrust-analyzer cargo zstd 0.13.3 stream/raw/Operation#finish().(&mut dst, self.finished_framerust-analyzer cargo zstd 0.13.3 stream/zio/reader/Reader#finished_frame.)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
187 // eprintln!("Hint: {} ; Output: {:?}", hint, dst);
188 if hint rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().0 {
189 // This indicates that the footer is complete.
190 // This is the only way to terminate the stream cleanly.
191 self.staterust-analyzer cargo zstd 0.13.3 stream/zio/reader/Reader#state. = Staterust-analyzer cargo zstd 0.13.3 stream/zio/reader/State#::Finishedrust-analyzer cargo zstd 0.13.3 stream/zio/reader/State#Finished#;
192 }
193
194 return Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(dst.posrust-analyzer cargo zstd-safe 7.2.2 impl#[`OutBuffer<'a, C>`]pos().());
195 }
196 Staterust-analyzer cargo zstd 0.13.3 stream/zio/reader/State#::Finishedrust-analyzer cargo zstd 0.13.3 stream/zio/reader/State#Finished# => {
197 return Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(0);
198 }
199 }
200 }
201 }
202 }
203
204 #[cfg(test)]
205 mod testsrust-analyzer cargo zstd 0.13.3 stream/zio/reader/tests/ {
206 use superrust-analyzer cargo zstd 0.13.3 stream/zio/reader/::Readerrust-analyzer cargo zstd 0.13.3 stream/zio/reader/Reader#;
207 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::{Cursorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/cursor/Cursor#, Readrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#};
208
209 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
210 fn test_nooprust-analyzer cargo zstd 0.13.3 stream/zio/reader/tests/test_noop().() {
211 use craterust-analyzer cargo zstd 0.13.3 crate/::streamrust-analyzer cargo zstd 0.13.3 stream/::rawrust-analyzer cargo zstd 0.13.3 stream/raw/::NoOprust-analyzer cargo zstd 0.13.3 stream/raw/NoOp#;
212
213 let input = b"AbcdefghAbcdefgh.";
214
215 // Test reader
216 let mut output = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().();
217 {
218 let mut reader = Readerrust-analyzer cargo zstd 0.13.3 stream/zio/reader/Reader#::newrust-analyzer cargo zstd 0.13.3 stream/zio/reader/impl#[`Reader<R, D>`]new().(Cursorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/cursor/Cursor#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/cursor/impl#[`Cursor<T>`]new().(input), NoOprust-analyzer cargo zstd 0.13.3 stream/raw/NoOp#);
219 reader.read_to_endrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#read_to_end().(&mut output).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
220 }
221 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(&output, input);
222 }
223
224 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
225 fn test_compressrust-analyzer cargo zstd 0.13.3 stream/zio/reader/tests/test_compress().() {
226 use craterust-analyzer cargo zstd 0.13.3 crate/::streamrust-analyzer cargo zstd 0.13.3 stream/::rawrust-analyzer cargo zstd 0.13.3 stream/raw/::Encoderrust-analyzer cargo zstd 0.13.3 stream/raw/Encoder#;
227
228 let input = b"AbcdefghAbcdefgh.";
229
230 // Test reader
231 let mut output = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().();
232 {
233 let mut reader =
234 Readerrust-analyzer cargo zstd 0.13.3 stream/zio/reader/Reader#::newrust-analyzer cargo zstd 0.13.3 stream/zio/reader/impl#[`Reader<R, D>`]new().(Cursorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/cursor/Cursor#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/cursor/impl#[`Cursor<T>`]new().(input), Encoderrust-analyzer cargo zstd 0.13.3 stream/raw/Encoder#::newrust-analyzer cargo zstd 0.13.3 stream/raw/impl#[`Encoder<'static>`]new().(1).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().());
235 reader.read_to_endrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#read_to_end().(&mut output).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
236 }
237 // eprintln!("{:?}", output);
238 let decoded = craterust-analyzer cargo zstd 0.13.3 crate/::decode_allrust-analyzer cargo zstd 0.13.3 stream/functions/decode_all().(&output[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFull#]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
239 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(&decoded, input);
240 }
241 }