| 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/, Writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#}; |
| 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 | // input -> [ zstd -> buffer -> writer ] |
| 6 | |
| 7 | /// Implements the [`Write`] API around an [`Operation`]. |
| 8 | /// |
| 9 | /// This can be used to wrap a raw in-memory operation in a write-focused API. |
| 10 | /// |
| 11 | /// It can be used with either compression or decompression, and forwards the |
| 12 | /// output to a wrapped `Write`. |
| 13 | pub struct Writerrust-analyzer cargo zstd 0.13.3 stream/zio/writer/Writer#<W, D> { |
| 14 | /// Either an encoder or a decoder. |
| 15 | operationrust-analyzer cargo zstd 0.13.3 stream/zio/writer/Writer#operation.: D, |
| 16 | |
| 17 | /// Where we send the output of the operation. |
| 18 | writerrust-analyzer cargo zstd 0.13.3 stream/zio/writer/Writer#writer.: W, |
| 19 | |
| 20 | /// Offset into the buffer |
| 21 | /// |
| 22 | /// Only things after this matter. Things before have already been sent to the writer. |
| 23 | offsetrust-analyzer cargo zstd 0.13.3 stream/zio/writer/Writer#offset.: usize, |
| 24 | |
| 25 | /// Output buffer |
| 26 | /// |
| 27 | /// Where the operation writes, before it gets flushed to the writer |
| 28 | bufferrust-analyzer cargo zstd 0.13.3 stream/zio/writer/Writer#buffer.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>, |
| 29 | |
| 30 | // When `true`, indicates that nothing should be added to the buffer. |
| 31 | // All that's left if to empty the buffer. |
| 32 | finishedrust-analyzer cargo zstd 0.13.3 stream/zio/writer/Writer#finished.: bool, |
| 33 | |
| 34 | /// When `true`, the operation just finished a frame. |
| 35 | /// |
| 36 | /// Only happens when decompressing. |
| 37 | /// The context needs to be re-initialized to process the next frame. |
| 38 | finished_framerust-analyzer cargo zstd 0.13.3 stream/zio/writer/Writer#finished_frame.: bool, |
| 39 | } |
| 40 | |
| 41 | impl<W, D> Writerrust-analyzer cargo zstd 0.13.3 stream/zio/writer/Writer#<W, D> |
| 42 | where |
| 43 | W: Writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#, |
| 44 | D: Operationrust-analyzer cargo zstd 0.13.3 stream/raw/Operation#, |
| 45 | { |
| 46 | /// Creates a new `Writer` with a fixed buffer capacity of 32KB |
| 47 | /// |
| 48 | /// All output from the given operation will be forwarded to `writer`. |
| 49 | pub fn newrust-analyzer cargo zstd 0.13.3 stream/zio/writer/impl#[`Writer<W, D>`]new().(writer: W, operation: D) -> Selfrust-analyzer cargo zstd 0.13.3 stream/zio/writer/impl#[`Writer<W, D>`] { |
| 50 | // 32KB buffer? That's what flate2 uses |
| 51 | Selfrust-analyzer cargo zstd 0.13.3 stream/zio/writer/impl#[`Writer<W, D>`]::new_with_capacityrust-analyzer cargo zstd 0.13.3 stream/zio/writer/impl#[`Writer<W, D>`]new_with_capacity().(writer, operation, 32 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().1024) |
| 52 | } |
| 53 | |
| 54 | /// Creates a new `Writer` with user defined capacity. |
| 55 | /// |
| 56 | /// All output from the given operation will be forwarded to `writer`. |
| 57 | pub fn new_with_capacityrust-analyzer cargo zstd 0.13.3 stream/zio/writer/impl#[`Writer<W, D>`]new_with_capacity().( |
| 58 | writer: W, |
| 59 | operation: D, |
| 60 | capacity: usize, |
| 61 | ) -> Selfrust-analyzer cargo zstd 0.13.3 stream/zio/writer/impl#[`Writer<W, D>`] { |
| 62 | Selfrust-analyzer cargo zstd 0.13.3 stream/zio/writer/impl#[`Writer<W, D>`]::with_output_bufferrust-analyzer cargo zstd 0.13.3 stream/zio/writer/impl#[`Writer<W, D>`]with_output_buffer().( |
| 63 | Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::with_capacityrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]with_capacity().(capacity), |
| 64 | writer, |
| 65 | operation, |
| 66 | ) |
| 67 | } |
| 68 | |
| 69 | /// Creates a new `Writer` using the given output buffer. |
| 70 | /// |
| 71 | /// The output buffer _must_ have pre-allocated capacity (its capacity will not be changed after). |
| 72 | /// |
| 73 | /// Usually you would use `Vec::with_capacity(desired_buffer_size)`. |
| 74 | pub fn with_output_bufferrust-analyzer cargo zstd 0.13.3 stream/zio/writer/impl#[`Writer<W, D>`]with_output_buffer().( |
| 75 | output_buffer: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>, |
| 76 | writer: W, |
| 77 | operation: D, |
| 78 | ) -> Selfrust-analyzer cargo zstd 0.13.3 stream/zio/writer/impl#[`Writer<W, D>`] { |
| 79 | Writerrust-analyzer cargo zstd 0.13.3 stream/zio/writer/Writer# { |
| 80 | writerrust-analyzer cargo zstd 0.13.3 stream/zio/writer/Writer#writer., |
| 81 | operationrust-analyzer cargo zstd 0.13.3 stream/zio/writer/Writer#operation., |
| 82 | |
| 83 | offsetrust-analyzer cargo zstd 0.13.3 stream/zio/writer/Writer#offset.: 0, |
| 84 | // 32KB buffer? That's what flate2 uses |
| 85 | bufferrust-analyzer cargo zstd 0.13.3 stream/zio/writer/Writer#buffer.: output_buffer, |
| 86 | |
| 87 | finishedrust-analyzer cargo zstd 0.13.3 stream/zio/writer/Writer#finished.: false, |
| 88 | finished_framerust-analyzer cargo zstd 0.13.3 stream/zio/writer/Writer#finished_frame.: false, |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | /// Ends the stream. |
| 93 | /// |
| 94 | /// This *must* be called after all data has been written to finish the |
| 95 | /// stream. |
| 96 | /// |
| 97 | /// If you forget to call this and just drop the `Writer`, you *will* have |
| 98 | /// an incomplete output. |
| 99 | /// |
| 100 | /// Keep calling it until it returns `Ok(())`, then don't call it again. |
| 101 | pub fn finishrust-analyzer cargo zstd 0.13.3 stream/zio/writer/impl#[`Writer<W, D>`]finish().(&mut self) -> 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#<()> { |
| 102 | loop { |
| 103 | // Keep trying until we're really done. |
| 104 | self.write_from_offsetrust-analyzer cargo zstd 0.13.3 stream/zio/writer/impl#[`Writer<W, D>`]write_from_offset().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 105 | |
| 106 | // At this point the buffer has been fully written out. |
| 107 | |
| 108 | if self.finishedrust-analyzer cargo zstd 0.13.3 stream/zio/writer/Writer#finished. { |
| 109 | return Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()); |
| 110 | } |
| 111 | |
| 112 | // Let's fill this buffer again! |
| 113 | |
| 114 | let finished_frame = self.finished_framerust-analyzer cargo zstd 0.13.3 stream/zio/writer/Writer#finished_frame.; |
| 115 | let hint = |
| 116 | self.with_bufferrust-analyzer cargo zstd 0.13.3 stream/zio/writer/impl#[`Writer<W, D>`]with_buffer().(|dst, op| op.finishrust-analyzer cargo zstd 0.13.3 stream/raw/Operation#finish().(dst, finished_frame)); |
| 117 | self.offsetrust-analyzer cargo zstd 0.13.3 stream/zio/writer/Writer#offset. = 0; |
| 118 | // println!("Hint: {:?}\nOut:{:?}", hint, &self.buffer); |
| 119 | |
| 120 | // We return here if zstd had a problem. |
| 121 | // Could happen with invalid data, ... |
| 122 | let hint = hint?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 123 | |
| 124 | if hint rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]ne().0 && self.bufferrust-analyzer cargo zstd 0.13.3 stream/zio/writer/Writer#buffer..is_emptyrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]is_empty().() { |
| 125 | // This happens if we are decoding an incomplete frame. |
| 126 | return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[Error]new().( |
| 127 | iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::ErrorKindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#::UnexpectedEofrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#UnexpectedEof#, |
| 128 | "incomplete frame", |
| 129 | )); |
| 130 | } |
| 131 | |
| 132 | // println!("Finishing {}, {}", bytes_written, hint); |
| 133 | |
| 134 | self.finishedrust-analyzer cargo zstd 0.13.3 stream/zio/writer/Writer#finished. = 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; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | /// Run the given closure on `self.buffer`. |
| 139 | /// |
| 140 | /// The buffer will be cleared, and made available wrapped in an `OutBuffer`. |
| 141 | fn with_bufferrust-analyzer cargo zstd 0.13.3 stream/zio/writer/impl#[`Writer<W, D>`]with_buffer().<F, T>(&mut self, f: F) -> T |
| 142 | where |
| 143 | F: FnOncerust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnOnce#(&mut OutBufferrust-analyzer cargo zstd-safe 7.2.2 OutBuffer#<'_, Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>>, &mut D) -> T, |
| 144 | { |
| 145 | self.bufferrust-analyzer cargo zstd 0.13.3 stream/zio/writer/Writer#buffer..clearrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]clear().(); |
| 146 | let mut output = OutBufferrust-analyzer cargo zstd-safe 7.2.2 OutBuffer#::aroundrust-analyzer cargo zstd-safe 7.2.2 impl#[`OutBuffer<'a, C>`]around().(&mut self.bufferrust-analyzer cargo zstd 0.13.3 stream/zio/writer/Writer#buffer.); |
| 147 | // eprintln!("Output: {:?}", output); |
| 148 | f(&mut output, &mut self.operationrust-analyzer cargo zstd 0.13.3 stream/zio/writer/Writer#operation.) |
| 149 | } |
| 150 | |
| 151 | /// Attempt to write `self.buffer` to the wrapped writer. |
| 152 | /// |
| 153 | /// Returns `Ok(())` once all the buffer has been written. |
| 154 | fn write_from_offsetrust-analyzer cargo zstd 0.13.3 stream/zio/writer/impl#[`Writer<W, D>`]write_from_offset().(&mut self) -> 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#<()> { |
| 155 | // The code looks a lot like `write_all`, but keeps track of what has |
| 156 | // been written in case we're interrupted. |
| 157 | while self.offsetrust-analyzer cargo zstd 0.13.3 stream/zio/writer/Writer#offset. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().self.bufferrust-analyzer cargo zstd 0.13.3 stream/zio/writer/Writer#buffer..lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().() { |
| 158 | match self.writerrust-analyzer cargo zstd 0.13.3 stream/zio/writer/Writer#writer..writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#write().(&self.bufferrust-analyzer cargo zstd 0.13.3 stream/zio/writer/Writer#buffer.[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().self.offsetrust-analyzer cargo zstd 0.13.3 stream/zio/writer/Writer#offset...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFrom#]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().) { |
| 159 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(0) => { |
| 160 | return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[Error]new().( |
| 161 | iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::ErrorKindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#::WriteZerorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#WriteZero#, |
| 162 | "writer will not accept any more data", |
| 163 | )) |
| 164 | } |
| 165 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(n) => self.offsetrust-analyzer cargo zstd 0.13.3 stream/zio/writer/Writer#offset. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().n, |
| 166 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(ref e) if e.kindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[Error]kind().() rust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[ErrorKind][`PartialEq<Self>`]eq().==rust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[ErrorKind][`PartialEq<Self>`]eq(). rust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[ErrorKind][`PartialEq<Self>`]eq().iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::ErrorKindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#::Interruptedrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#Interrupted# => (), |
| 167 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(e) => return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(e), |
| 168 | } |
| 169 | } |
| 170 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 171 | } |
| 172 | |
| 173 | /// Return the wrapped `Writer` and `Operation`. |
| 174 | /// |
| 175 | /// Careful: if you call this before calling [`Writer::finish()`], the |
| 176 | /// output may be incomplete. |
| 177 | pub fn into_innerrust-analyzer cargo zstd 0.13.3 stream/zio/writer/impl#[`Writer<W, D>`]into_inner().(self) -> (W, D) { |
| 178 | (self.writerrust-analyzer cargo zstd 0.13.3 stream/zio/writer/Writer#writer., self.operationrust-analyzer cargo zstd 0.13.3 stream/zio/writer/Writer#operation.) |
| 179 | } |
| 180 | |
| 181 | /// Gives a reference to the inner writer. |
| 182 | pub fn writerrust-analyzer cargo zstd 0.13.3 stream/zio/writer/impl#[`Writer<W, D>`]writer().(&self) -> &W { |
| 183 | &self.writerrust-analyzer cargo zstd 0.13.3 stream/zio/writer/Writer#writer. |
| 184 | } |
| 185 | |
| 186 | /// Gives a mutable reference to the inner writer. |
| 187 | pub fn writer_mutrust-analyzer cargo zstd 0.13.3 stream/zio/writer/impl#[`Writer<W, D>`]writer_mut().(&mut self) -> &mut W { |
| 188 | &mut self.writerrust-analyzer cargo zstd 0.13.3 stream/zio/writer/Writer#writer. |
| 189 | } |
| 190 | |
| 191 | /// Gives a reference to the inner operation. |
| 192 | pub fn operationrust-analyzer cargo zstd 0.13.3 stream/zio/writer/impl#[`Writer<W, D>`]operation().(&self) -> &D { |
| 193 | &self.operationrust-analyzer cargo zstd 0.13.3 stream/zio/writer/Writer#operation. |
| 194 | } |
| 195 | |
| 196 | /// Gives a mutable reference to the inner operation. |
| 197 | pub fn operation_mutrust-analyzer cargo zstd 0.13.3 stream/zio/writer/impl#[`Writer<W, D>`]operation_mut().(&mut self) -> &mut D { |
| 198 | &mut self.operationrust-analyzer cargo zstd 0.13.3 stream/zio/writer/Writer#operation. |
| 199 | } |
| 200 | |
| 201 | /// Returns the offset in the current buffer. Only useful for debugging. |
| 202 | #[cfg(test)] |
| 203 | pub fn offsetrust-analyzer cargo zstd 0.13.3 stream/zio/writer/impl#[`Writer<W, D>`]offset().(&self) -> usize { |
| 204 | self.offsetrust-analyzer cargo zstd 0.13.3 stream/zio/writer/Writer#offset. |
| 205 | } |
| 206 | |
| 207 | /// Returns the current buffer. Only useful for debugging. |
| 208 | #[cfg(test)] |
| 209 | pub fn bufferrust-analyzer cargo zstd 0.13.3 stream/zio/writer/impl#[`Writer<W, D>`]buffer().(&self) -> &[u8] { |
| 210 | &self.bufferrust-analyzer cargo zstd 0.13.3 stream/zio/writer/Writer#buffer. |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | impl<W, D> Writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write# for Writerrust-analyzer cargo zstd 0.13.3 stream/zio/writer/Writer#<W, D> |
| 215 | where |
| 216 | W: Writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#, |
| 217 | D: Operationrust-analyzer cargo zstd 0.13.3 stream/raw/Operation#, |
| 218 | { |
| 219 | fn writerust-analyzer cargo zstd 0.13.3 stream/zio/writer/impl#[`Writer<W, D>`][Write]write().(&mut self, buf: &[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> { |
| 220 | if self.finishedrust-analyzer cargo zstd 0.13.3 stream/zio/writer/Writer#finished. { |
| 221 | return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[Error]new().( |
| 222 | iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::ErrorKindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#::Otherrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#Other#, |
| 223 | "encoder is finished", |
| 224 | )); |
| 225 | } |
| 226 | // Keep trying until _something_ has been consumed. |
| 227 | // As soon as some input has been taken, we cannot afford |
| 228 | // to take any chance: if an error occurs, the user couldn't know |
| 229 | // that some data _was_ successfully written. |
| 230 | loop { |
| 231 | // First, write any pending data from `self.buffer`. |
| 232 | self.write_from_offsetrust-analyzer cargo zstd 0.13.3 stream/zio/writer/impl#[`Writer<W, D>`]write_from_offset().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 233 | // At this point `self.buffer` can safely be discarded. |
| 234 | |
| 235 | // Support writing concatenated frames by re-initializing the |
| 236 | // context. |
| 237 | if self.finished_framerust-analyzer cargo zstd 0.13.3 stream/zio/writer/Writer#finished_frame. { |
| 238 | self.operationrust-analyzer cargo zstd 0.13.3 stream/zio/writer/Writer#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().; |
| 239 | self.finished_framerust-analyzer cargo zstd 0.13.3 stream/zio/writer/Writer#finished_frame. = false; |
| 240 | } |
| 241 | |
| 242 | let mut src = InBufferrust-analyzer cargo zstd-safe 7.2.2 InBuffer#::aroundrust-analyzer cargo zstd-safe 7.2.2 impl#[`InBuffer<'a>`]around().(buf); |
| 243 | let hint = self.with_bufferrust-analyzer cargo zstd 0.13.3 stream/zio/writer/impl#[`Writer<W, D>`]with_buffer().(|dst, op| op.runrust-analyzer cargo zstd 0.13.3 stream/raw/Operation#run().(&mut src, dst)); |
| 244 | let bytes_read = src.posrust-analyzer cargo zstd-safe 7.2.2 InBuffer#pos.; |
| 245 | |
| 246 | // eprintln!( |
| 247 | // "Write Hint: {:?}\n src: {:?}\n dst: {:?}", |
| 248 | // hint, src, self.buffer |
| 249 | // ); |
| 250 | |
| 251 | self.offsetrust-analyzer cargo zstd 0.13.3 stream/zio/writer/Writer#offset. = 0; |
| 252 | let hint = hint?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 253 | |
| 254 | 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 { |
| 255 | self.finished_framerust-analyzer cargo zstd 0.13.3 stream/zio/writer/Writer#finished_frame. = true; |
| 256 | } |
| 257 | |
| 258 | // As we said, as soon as we've consumed something, return. |
| 259 | if bytes_read 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 || buf.is_emptyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]is_empty().() { |
| 260 | // println!("Returning {}", bytes_read); |
| 261 | return Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(bytes_read); |
| 262 | } |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | fn flushrust-analyzer cargo zstd 0.13.3 stream/zio/writer/impl#[`Writer<W, D>`][Write]flush().(&mut self) -> 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#<()> { |
| 267 | let mut finished = self.finishedrust-analyzer cargo zstd 0.13.3 stream/zio/writer/Writer#finished.; |
| 268 | loop { |
| 269 | // If the output is blocked or has an error, return now. |
| 270 | self.write_from_offsetrust-analyzer cargo zstd 0.13.3 stream/zio/writer/impl#[`Writer<W, D>`]write_from_offset().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 271 | |
| 272 | if finished { |
| 273 | break; |
| 274 | } |
| 275 | |
| 276 | let hint = self.with_bufferrust-analyzer cargo zstd 0.13.3 stream/zio/writer/impl#[`Writer<W, D>`]with_buffer().(|dst, op| op.flushrust-analyzer cargo zstd 0.13.3 stream/raw/Operation#flush().(dst)); |
| 277 | |
| 278 | self.offsetrust-analyzer cargo zstd 0.13.3 stream/zio/writer/Writer#offset. = 0; |
| 279 | let hint = hint?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 280 | |
| 281 | finished = 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; |
| 282 | } |
| 283 | |
| 284 | self.writerrust-analyzer cargo zstd 0.13.3 stream/zio/writer/Writer#writer..flushrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#flush().() |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | #[cfg(test)] |
| 289 | mod testsrust-analyzer cargo zstd 0.13.3 stream/zio/writer/tests/ { |
| 290 | use superrust-analyzer cargo zstd 0.13.3 stream/zio/writer/::Writerrust-analyzer cargo zstd 0.13.3 stream/zio/writer/Writer#; |
| 291 | 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/::Writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#; |
| 292 | |
| 293 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 294 | fn test_nooprust-analyzer cargo zstd 0.13.3 stream/zio/writer/tests/test_noop().() { |
| 295 | 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#; |
| 296 | |
| 297 | let input = b"AbcdefghAbcdefgh."; |
| 298 | |
| 299 | // Test writer |
| 300 | 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().(); |
| 301 | { |
| 302 | let mut writer = Writerrust-analyzer cargo zstd 0.13.3 stream/zio/writer/Writer#::newrust-analyzer cargo zstd 0.13.3 stream/zio/writer/impl#[`Writer<W, D>`]new().(&mut output, NoOprust-analyzer cargo zstd 0.13.3 stream/raw/NoOp#); |
| 303 | writer.write_allrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#write_all().(input).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 304 | writer.finishrust-analyzer cargo zstd 0.13.3 stream/zio/writer/impl#[`Writer<W, D>`]finish().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 305 | } |
| 306 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(&output, input); |
| 307 | } |
| 308 | |
| 309 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 310 | fn test_compressrust-analyzer cargo zstd 0.13.3 stream/zio/writer/tests/test_compress().() { |
| 311 | 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#; |
| 312 | |
| 313 | let input = b"AbcdefghAbcdefgh."; |
| 314 | |
| 315 | // Test writer |
| 316 | 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().(); |
| 317 | { |
| 318 | let mut writer = |
| 319 | Writerrust-analyzer cargo zstd 0.13.3 stream/zio/writer/Writer#::newrust-analyzer cargo zstd 0.13.3 stream/zio/writer/impl#[`Writer<W, D>`]new().(&mut output, 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().()); |
| 320 | writer.write_allrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#write_all().(input).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 321 | writer.finishrust-analyzer cargo zstd 0.13.3 stream/zio/writer/impl#[`Writer<W, D>`]finish().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 322 | } |
| 323 | // println!("Output: {:?}", output); |
| 324 | 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().(); |
| 325 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(&decoded, input); |
| 326 | } |
| 327 | |
| 328 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 329 | fn test_compress_with_capacityrust-analyzer cargo zstd 0.13.3 stream/zio/writer/tests/test_compress_with_capacity().() { |
| 330 | 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#; |
| 331 | |
| 332 | let input = b"AbcdefghAbcdefgh."; |
| 333 | |
| 334 | // Test writer |
| 335 | 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().(); |
| 336 | { |
| 337 | let mut writer = Writerrust-analyzer cargo zstd 0.13.3 stream/zio/writer/Writer#::new_with_capacityrust-analyzer cargo zstd 0.13.3 stream/zio/writer/impl#[`Writer<W, D>`]new_with_capacity().( |
| 338 | &mut output, |
| 339 | 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().(), |
| 340 | 64, |
| 341 | ); |
| 342 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(writer.bufferrust-analyzer cargo zstd 0.13.3 stream/zio/writer/Writer#buffer..capacityrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]capacity().(), 64); |
| 343 | writer.write_allrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#write_all().(input).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 344 | writer.finishrust-analyzer cargo zstd 0.13.3 stream/zio/writer/impl#[`Writer<W, D>`]finish().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 345 | } |
| 346 | 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().(); |
| 347 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(&decoded, input); |
| 348 | } |
| 349 | |
| 350 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 351 | fn test_decompressrust-analyzer cargo zstd 0.13.3 stream/zio/writer/tests/test_decompress().() { |
| 352 | 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/::Decoderrust-analyzer cargo zstd 0.13.3 stream/raw/Decoder#; |
| 353 | |
| 354 | let input = b"AbcdefghAbcdefgh."; |
| 355 | let compressed = craterust-analyzer cargo zstd 0.13.3 crate/::encode_allrust-analyzer cargo zstd 0.13.3 stream/functions/encode_all().(&input[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFull#]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()., 1).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 356 | |
| 357 | // Test writer |
| 358 | 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().(); |
| 359 | { |
| 360 | let mut writer = Writerrust-analyzer cargo zstd 0.13.3 stream/zio/writer/Writer#::newrust-analyzer cargo zstd 0.13.3 stream/zio/writer/impl#[`Writer<W, D>`]new().(&mut output, Decoderrust-analyzer cargo zstd 0.13.3 stream/raw/Decoder#::newrust-analyzer cargo zstd 0.13.3 stream/raw/impl#[`Decoder<'static>`]new().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().()); |
| 361 | writer.write_allrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#write_all().(&compressed).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 362 | writer.finishrust-analyzer cargo zstd 0.13.3 stream/zio/writer/impl#[`Writer<W, D>`]finish().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 363 | } |
| 364 | // println!("Output: {:?}", output); |
| 365 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(&output, input); |
| 366 | } |
| 367 | |
| 368 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 369 | fn test_decompress_with_capacityrust-analyzer cargo zstd 0.13.3 stream/zio/writer/tests/test_decompress_with_capacity().() { |
| 370 | 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/::Decoderrust-analyzer cargo zstd 0.13.3 stream/raw/Decoder#; |
| 371 | |
| 372 | let input = b"AbcdefghAbcdefgh."; |
| 373 | let compressed = craterust-analyzer cargo zstd 0.13.3 crate/::encode_allrust-analyzer cargo zstd 0.13.3 stream/functions/encode_all().(&input[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFull#]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()., 1).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 374 | |
| 375 | // Test writer |
| 376 | 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().(); |
| 377 | { |
| 378 | let mut writer = Writerrust-analyzer cargo zstd 0.13.3 stream/zio/writer/Writer#::new_with_capacityrust-analyzer cargo zstd 0.13.3 stream/zio/writer/impl#[`Writer<W, D>`]new_with_capacity().( |
| 379 | &mut output, |
| 380 | Decoderrust-analyzer cargo zstd 0.13.3 stream/raw/Decoder#::newrust-analyzer cargo zstd 0.13.3 stream/raw/impl#[`Decoder<'static>`]new().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(), |
| 381 | 64, |
| 382 | ); |
| 383 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(writer.bufferrust-analyzer cargo zstd 0.13.3 stream/zio/writer/Writer#buffer..capacityrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]capacity().(), 64); |
| 384 | writer.write_allrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#write_all().(&compressed).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 385 | writer.finishrust-analyzer cargo zstd 0.13.3 stream/zio/writer/impl#[`Writer<W, D>`]finish().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 386 | } |
| 387 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(&output, input); |
| 388 | } |
| 389 | } |