Debian Codegraph @

.cargo/vendor/weezl/src/decode_into_async.rs

raw
1 use crate::decode::IntoAsync;
2 use crate::error::LzwStatus;
3 use crate::error::StreamResult;
4 use crate::StreamBuf;
5 use std::io;
6
7 impl<'d, W: futures::io::AsyncWrite + core::marker::Unpin> IntoAsync<'d, W> {
8 /// Decode data from a reader.
9 ///
10 /// This will read data until the stream is empty or an end marker is reached.
11 pub async fn decode(&mut self, read: impl futures::io::AsyncBufRead) -> StreamResult {
12 self.decode_part(read, false).await
13 }
14
15 /// Decode data from a reader, requiring an end marker.
16 pub async fn decode_all(mut self, read: impl futures::io::AsyncBufRead) -> StreamResult {
17 self.decode_part(read, true).await
18 }
19
20 /// Set the size of the intermediate decode buffer.
21 ///
22 /// A buffer of this size is allocated to hold one part of the decoded stream when no buffer is
23 /// available and any decoding method is called. No buffer is allocated if `set_buffer` has
24 /// been called. The buffer is reused.
25 ///
26 /// # Panics
27 /// This method panics if `size` is `0`.
28 pub fn set_buffer_size(&mut self, size: usize) {
29 assert_ne!(size, 0, "Attempted to set empty buffer");
30 self.default_size = size;
31 }
32
33 /// Use a particular buffer as an intermediate decode buffer.
34 ///
35 /// Calling this sets or replaces the buffer. When a buffer has been set then it is used
36 /// instead of dynamically allocating a buffer. Note that the size of the buffer is critical
37 /// for efficient decoding. Some optimization techniques require the buffer to hold one or more
38 /// previous decoded words. There is also additional overhead from `write` calls each time the
39 /// buffer has been filled.
40 ///
41 /// # Panics
42 /// This method panics if the `buffer` is empty.
43 pub fn set_buffer(&mut self, buffer: &'d mut [u8]) {
44 assert_ne!(buffer.len(), 0, "Attempted to set empty buffer");
45 self.buffer = Some(StreamBuf::Borrowed(buffer));
46 }
47
48 async fn decode_part(
49 &mut self,
50 read: impl futures::io::AsyncBufRead,
51 must_finish: bool,
52 ) -> StreamResult {
53 use futures::io::AsyncBufReadExt;
54 use futures::io::AsyncWriteExt;
55
56 let IntoAsync {
57 decoder,
58 writer,
59 buffer,
60 default_size,
61 } = self;
62
63 futures::pin_mut!(read);
64 let mut read: core::pin::Pin<_> = read;
65
66 let mut bytes_read = 0;
67 let mut bytes_written = 0;
68
69 // Converting to mutable refs to move into the `once` closure.
70 let read_bytes = &mut bytes_read;
71 let write_bytes = &mut bytes_written;
72
73 let outbuf: &mut [u8] =
74 match { buffer.get_or_insert_with(|| StreamBuf::Owned(vec![0u8; *default_size])) } {
75 StreamBuf::Borrowed(slice) => &mut *slice,
76 StreamBuf::Owned(vec) => &mut *vec,
77 };
78 assert!(!outbuf.is_empty());
79
80 let status = loop {
81 // Try to grab one buffer of input data.
82 let mut filler = read.as_mut();
83 let data = match filler.fill_buf().await {
84 Ok(buf) => buf,
85 Err(err) => break Err(err),
86 };
87
88 // Decode as much of the buffer as fits.
89 let result = decoder.decode_bytes(data, &mut outbuf[..]);
90 // Do the bookkeeping and consume the buffer.
91 *read_bytes += result.consumed_in;
92 *write_bytes += result.consumed_out;
93 read.as_mut().consume(result.consumed_in);
94
95 // Handle an error status in the result.
96 let status = match result.status {
97 Ok(ok) => ok,
98 Err(err) => {
99 break Err(io::Error::new(
100 io::ErrorKind::InvalidData,
101 &*format!("{:?}", err),
102 ));
103 }
104 };
105
106 // Check if we had any new data at all.
107 if let LzwStatus::NoProgress = status {
108 debug_assert_eq!(
109 result.consumed_out, 0,
110 "No progress means we have not decoded any data"
111 );
112 // In particular we did not finish decoding.
113 if must_finish {
114 break Err(io::Error::new(
115 io::ErrorKind::UnexpectedEof,
116 "No more data but no end marker detected",
117 ));
118 } else {
119 break Ok(());
120 }
121 }
122
123 // And finish by writing our result.
124 // TODO: we may lose data on error (also on status error above) which we might want to
125 // deterministically handle so that we don't need to restart everything from scratch as
126 // the only recovery strategy. Any changes welcome.
127 match writer.write_all(&outbuf[..result.consumed_out]).await {
128 Ok(_) => {}
129 Err(err) => break Err(err),
130 }
131
132 if let LzwStatus::Done = status {
133 break Ok(());
134 }
135 };
136
137 StreamResult {
138 bytes_read,
139 bytes_written,
140 status,
141 }
142 }
143 }