|
2 | 2 | // MIT License (https://github.com/Majored/rs-async-zip/blob/main/LICENSE)
|
3 | 3 |
|
4 | 4 | use crate::error::{Result, ZipError};
|
5 |
| -use futures_lite::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt}; |
| 5 | +use futures_lite::io::{AsyncRead, AsyncReadExt}; |
6 | 6 |
|
7 | 7 | // Assert that the next four-byte signature read by a reader which impls AsyncRead matches the expected signature.
|
8 |
| -#[tracing::instrument(skip(reader))] |
9 |
| -pub(crate) async fn assert_signature(reader: impl AsyncRead + Unpin, expected: u32) -> Result<()> { |
10 |
| - let actual = read_u32(reader).await?; |
11 |
| - |
12 |
| - if actual != expected { |
13 |
| - return Err(ZipError::UnexpectedHeaderError(actual, expected)); |
| 8 | +pub(crate) async fn assert_signature<R: AsyncRead + Unpin>(reader: &mut R, expected: u32) -> Result<()> { |
| 9 | + let signature = { |
| 10 | + let mut buffer = [0; 4]; |
| 11 | + reader.read_exact(&mut buffer).await?; |
| 12 | + u32::from_le_bytes(buffer) |
| 13 | + }; |
| 14 | + match signature { |
| 15 | + actual if actual == expected => Ok(()), |
| 16 | + actual => Err(ZipError::UnexpectedHeaderError(actual, expected)), |
14 | 17 | }
|
15 |
| - |
16 |
| - Ok(()) |
17 |
| -} |
18 |
| - |
19 |
| -#[tracing::instrument(skip(reader))] |
20 |
| -pub(crate) async fn read_u16(mut reader: impl AsyncRead + Unpin) -> Result<u16> { |
21 |
| - let mut buf = [0u8; 2]; |
22 |
| - reader.read_exact(&mut buf).await.unwrap(); |
23 |
| - Ok(u16::from_le_bytes(buf)) |
24 |
| -} |
25 |
| - |
26 |
| -#[tracing::instrument(skip(reader))] |
27 |
| -pub(crate) async fn read_u32(mut reader: impl AsyncRead + Unpin) -> Result<u32> { |
28 |
| - let mut buf = [0u8; 4]; |
29 |
| - reader.read_exact(&mut buf).await.unwrap(); |
30 |
| - Ok(u32::from_le_bytes(buf)) |
31 |
| -} |
32 |
| - |
33 |
| -#[tracing::instrument(skip(writer))] |
34 |
| -pub(crate) async fn write_u16(mut writer: impl AsyncWrite + Unpin, value: u16) -> Result<()> { |
35 |
| - writer.write_all(&value.to_be_bytes()).await?; |
36 |
| - Ok(()) |
37 |
| -} |
38 |
| - |
39 |
| -#[tracing::instrument(skip(writer))] |
40 |
| -pub(crate) async fn write_u32(mut writer: impl AsyncWrite + Unpin, value: u32) -> Result<()> { |
41 |
| - writer.write_all(&value.to_be_bytes()).await?; |
42 |
| - Ok(()) |
43 |
| -} |
44 |
| - |
45 |
| -/// Read and return a dynamic length vector of bytes from a reader which impls AsyncRead. |
46 |
| -#[tracing::instrument(skip(reader))] |
47 |
| -pub(crate) async fn read_bytes(reader: impl AsyncRead + Unpin, length: usize) -> Result<Vec<u8>> { |
48 |
| - let mut buffer = Vec::with_capacity(length); |
49 |
| - reader.take(length as u64).read_to_end(&mut buffer).await?; |
50 |
| - Ok(buffer) |
51 | 18 | }
|
0 commit comments