Debian Codegraph @

src/base/read/mem.rs

raw symbols SCIP
1 // Copyright (c) 2022 Harry [Majored] [[email protected]]
2 // MIT License (https://github.com/Majored/rs-async-zip/blob/main/LICENSE)
3
4 //! A concurrent ZIP reader which acts over an owned vector of bytes.
5 //!
6 //! Concurrency is achieved as a result of:
7 //! - Wrapping the provided vector of bytes within an [`Arc`] to allow shared ownership.
8 //! - Wrapping this [`Arc`] around a [`Cursor`] when reading (as the [`Arc`] can deref and coerce into a `&[u8]`).
9 //!
10 //! ### Usage
11 //! Unlike the [`seek`] module, we no longer hold a mutable reference to any inner reader which in turn, allows the
12 //! construction of concurrent [`ZipEntryReader`]s. Though, note that each individual [`ZipEntryReader`] cannot be sent
13 //! between thread boundaries due to the masked lifetime requirement. Therefore, the overarching [`ZipFileReader`]
14 //! should be cloned and moved into those contexts when needed.
15 //!
16 //! ### Concurrent Example
17 //! ```no_run
18 //! # use async_zip::base::read::mem::ZipFileReader;
19 //! # use async_zip::error::Result;
20 //! # use futures_lite::io::AsyncReadExt;
21 //! #
22 //! async fn run() -> Result<()> {
23 //! let reader = ZipFileReader::new(Vec::new()).await?;
24 //! let result = tokio::join!(read(&reader, 0), read(&reader, 1));
25 //!
26 //! let data_0 = result.0?;
27 //! let data_1 = result.1?;
28 //!
29 //! // Use data within current scope.
30 //!
31 //! Ok(())
32 //! }
33 //!
34 //! async fn read(reader: &ZipFileReader, index: usize) -> Result<Vec<u8>> {
35 //! let mut entry = reader.reader_without_entry(index).await?;
36 //! let mut data = Vec::new();
37 //! entry.read_to_end(&mut data).await?;
38 //! Ok(data)
39 //! }
40 //! ```
41 //!
42 //! ### Parallel Example
43 //! ```no_run
44 //! # use async_zip::base::read::mem::ZipFileReader;
45 //! # use async_zip::error::Result;
46 //! # use futures_lite::io::AsyncReadExt;
47 //! #
48 //! async fn run() -> Result<()> {
49 //! let reader = ZipFileReader::new(Vec::new()).await?;
50 //!
51 //! let handle_0 = tokio::spawn(read(reader.clone(), 0));
52 //! let handle_1 = tokio::spawn(read(reader.clone(), 1));
53 //!
54 //! let data_0 = handle_0.await.expect("thread panicked")?;
55 //! let data_1 = handle_1.await.expect("thread panicked")?;
56 //!
57 //! // Use data within current scope.
58 //!
59 //! Ok(())
60 //! }
61 //!
62 //! async fn read(reader: ZipFileReader, index: usize) -> Result<Vec<u8>> {
63 //! let mut entry = reader.reader_without_entry(index).await?;
64 //! let mut data = Vec::new();
65 //! entry.read_to_end(&mut data).await?;
66 //! Ok(data)
67 //! }
68 //! ```
69
70 #[cfg(doc)]
71 use craterust-analyzer cargo async_zip 0.0.18 crate/::baserust-analyzer cargo async_zip 0.0.18 base/::readrust-analyzer cargo async_zip 0.0.18 base/read/::seekrust-analyzer cargo async_zip 0.0.18 base/read/seek/;
72
73 use craterust-analyzer cargo async_zip 0.0.18 crate/::baserust-analyzer cargo async_zip 0.0.18 base/::readrust-analyzer cargo async_zip 0.0.18 base/read/::iorust-analyzer cargo async_zip 0.0.18 base/read/io/::entryrust-analyzer cargo async_zip 0.0.18 base/read/io/entry/::ZipEntryReaderrust-analyzer cargo async_zip 0.0.18 base/read/io/entry/ZipEntryReader#;
74 use craterust-analyzer cargo async_zip 0.0.18 crate/::errorrust-analyzer cargo async_zip 0.0.18 error/::{Resultrust-analyzer cargo async_zip 0.0.18 error/Result#, ZipErrorrust-analyzer cargo async_zip 0.0.18 error/ZipError#};
75 use craterust-analyzer cargo async_zip 0.0.18 crate/::filerust-analyzer cargo async_zip 0.0.18 file/::ZipFilerust-analyzer cargo async_zip 0.0.18 file/ZipFile#;
76
77 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::syncrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/::Arcrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc sync/Arc#;
78
79 use futures_literust-analyzer cargo futures-lite 2.1.0 crate/::iorust-analyzer cargo futures-lite 2.1.0 io/::Cursorrust-analyzer cargo futures-lite 2.1.0 io/Cursor#;
80
81 use superrust-analyzer cargo async_zip 0.0.18 base/read/::iorust-analyzer cargo async_zip 0.0.18 base/read/io/::entryrust-analyzer cargo async_zip 0.0.18 base/read/io/entry/::{WithEntryrust-analyzer cargo async_zip 0.0.18 base/read/io/entry/WithEntry#, WithoutEntryrust-analyzer cargo async_zip 0.0.18 base/read/io/entry/WithoutEntry#};
82
83 struct Innerrust-analyzer cargo async_zip 0.0.18 base/read/mem/Inner# {
84 datarust-analyzer cargo async_zip 0.0.18 base/read/mem/Inner#data.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>,
85 filerust-analyzer cargo async_zip 0.0.18 base/read/mem/Inner#file.: ZipFilerust-analyzer cargo async_zip 0.0.18 file/ZipFile#,
86 }
87
88 // A concurrent ZIP reader which acts over an owned vector of bytes.
89 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:)]
90 pub struct ZipFileReaderrust-analyzer cargo async_zip 0.0.18 base/read/mem/ZipFileReader# {
91 innerrust-analyzer cargo async_zip 0.0.18 base/read/mem/ZipFileReader#inner.: Arcrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc sync/Arc#<Innerrust-analyzer cargo async_zip 0.0.18 base/read/mem/Inner#>,
92 }
93
94 impl ZipFileReaderrust-analyzer cargo async_zip 0.0.18 base/read/mem/ZipFileReader# {
95 /// Constructs a new ZIP reader from an owned vector of bytes.
96 pub async fn newrust-analyzer cargo async_zip 0.0.18 base/read/mem/impl#[ZipFileReader]new().(data: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>) -> Resultrust-analyzer cargo async_zip 0.0.18 error/Result#<ZipFileReaderrust-analyzer cargo async_zip 0.0.18 base/read/mem/ZipFileReader#> {
97 let file = craterust-analyzer cargo async_zip 0.0.18 crate/::baserust-analyzer cargo async_zip 0.0.18 base/::readrust-analyzer cargo async_zip 0.0.18 base/read/::filerust-analyzer cargo async_zip 0.0.18 base/read/file().(Cursorrust-analyzer cargo futures-lite 2.1.0 io/Cursor#::newrust-analyzer cargo futures-lite 2.1.0 io/impl#[`Cursor<T>`]new().(&data)).rust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().awaitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
98 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(ZipFileReaderrust-analyzer cargo async_zip 0.0.18 base/read/mem/ZipFileReader#::from_raw_partsrust-analyzer cargo async_zip 0.0.18 base/read/mem/impl#[ZipFileReader]from_raw_parts().(data, file))
99 }
100
101 /// Constructs a ZIP reader from an owned vector of bytes and ZIP file information derived from those bytes.
102 ///
103 /// Providing a [`ZipFile`] that wasn't derived from those bytes may lead to inaccurate parsing.
104 pub fn from_raw_partsrust-analyzer cargo async_zip 0.0.18 base/read/mem/impl#[ZipFileReader]from_raw_parts().(data: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>, file: ZipFilerust-analyzer cargo async_zip 0.0.18 file/ZipFile#) -> ZipFileReaderrust-analyzer cargo async_zip 0.0.18 base/read/mem/ZipFileReader# {
105 ZipFileReaderrust-analyzer cargo async_zip 0.0.18 base/read/mem/ZipFileReader# { innerrust-analyzer cargo async_zip 0.0.18 base/read/mem/ZipFileReader#inner.: Arcrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc sync/Arc#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc sync/impl#[`Arc<T>`]new().(Innerrust-analyzer cargo async_zip 0.0.18 base/read/mem/Inner# { datarust-analyzer cargo async_zip 0.0.18 base/read/mem/Inner#data., filerust-analyzer cargo async_zip 0.0.18 base/read/mem/Inner#file. }) }
106 }
107
108 /// Returns this ZIP file's information.
109 pub fn filerust-analyzer cargo async_zip 0.0.18 base/read/mem/impl#[ZipFileReader]file().(&self) -> &ZipFilerust-analyzer cargo async_zip 0.0.18 file/ZipFile# {
110 &self.innerrust-analyzer cargo async_zip 0.0.18 base/read/mem/ZipFileReader#inner..filerust-analyzer cargo async_zip 0.0.18 base/read/mem/Inner#file.
111 }
112
113 /// Returns the raw bytes provided to the reader during construction.
114 pub fn datarust-analyzer cargo async_zip 0.0.18 base/read/mem/impl#[ZipFileReader]data().(&self) -> &[u8] {
115 &self.innerrust-analyzer cargo async_zip 0.0.18 base/read/mem/ZipFileReader#inner..datarust-analyzer cargo async_zip 0.0.18 base/read/mem/Inner#data.
116 }
117
118 /// Returns a new entry reader if the provided index is valid.
119 pub async fn reader_without_entryrust-analyzer cargo async_zip 0.0.18 base/read/mem/impl#[ZipFileReader]reader_without_entry().(&self, index: usize) -> Resultrust-analyzer cargo async_zip 0.0.18 error/Result#<ZipEntryReaderrust-analyzer cargo async_zip 0.0.18 base/read/io/entry/ZipEntryReader#<Cursorrust-analyzer cargo futures-lite 2.1.0 io/Cursor#<&[u8]>, WithoutEntryrust-analyzer cargo async_zip 0.0.18 base/read/io/entry/WithoutEntry#>> {
120 let stored_entry = self.innerrust-analyzer cargo async_zip 0.0.18 base/read/mem/ZipFileReader#inner..filerust-analyzer cargo async_zip 0.0.18 base/read/mem/Inner#file..entriesrust-analyzer cargo async_zip 0.0.18 file/ZipFile#entries..getrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]get().(index).ok_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]ok_or().(ZipErrorrust-analyzer cargo async_zip 0.0.18 error/ZipError#::EntryIndexOutOfBoundsrust-analyzer cargo async_zip 0.0.18 error/ZipError#EntryIndexOutOfBounds#)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
121 let mut cursor = Cursorrust-analyzer cargo futures-lite 2.1.0 io/Cursor#::newrust-analyzer cargo futures-lite 2.1.0 io/impl#[`Cursor<T>`]new().(&self.innerrust-analyzer cargo async_zip 0.0.18 base/read/mem/ZipFileReader#inner..datarust-analyzer cargo async_zip 0.0.18 base/read/mem/Inner#data.[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().);
122
123 stored_entry.seek_to_data_offsetrust-analyzer cargo async_zip 0.0.18 entry/impl#[StoredZipEntry]seek_to_data_offset().(&mut cursor).rust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().awaitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
124
125 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(ZipEntryReaderrust-analyzer cargo async_zip 0.0.18 base/read/io/entry/ZipEntryReader#::new_with_ownedrust-analyzer cargo async_zip 0.0.18 base/read/io/entry/impl#[`ZipEntryReader<'a, R, WithoutEntry>`]new_with_owned().(
126 cursor,
127 stored_entry.entryrust-analyzer cargo async_zip 0.0.18 entry/StoredZipEntry#entry..compressionrust-analyzer cargo async_zip 0.0.18 entry/impl#[ZipEntry]compression().(),
128 stored_entry.entryrust-analyzer cargo async_zip 0.0.18 entry/StoredZipEntry#entry..compressed_sizerust-analyzer cargo async_zip 0.0.18 entry/impl#[ZipEntry]compressed_size().(),
129 ))
130 }
131
132 /// Returns a new entry reader if the provided index is valid.
133 pub async fn reader_with_entryrust-analyzer cargo async_zip 0.0.18 base/read/mem/impl#[ZipFileReader]reader_with_entry().(&self, index: usize) -> Resultrust-analyzer cargo async_zip 0.0.18 error/Result#<ZipEntryReaderrust-analyzer cargo async_zip 0.0.18 base/read/io/entry/ZipEntryReader#<Cursorrust-analyzer cargo futures-lite 2.1.0 io/Cursor#<&[u8]>, WithEntryrust-analyzer cargo async_zip 0.0.18 base/read/io/entry/WithEntry#<'_>>> {
134 let stored_entry = self.innerrust-analyzer cargo async_zip 0.0.18 base/read/mem/ZipFileReader#inner..filerust-analyzer cargo async_zip 0.0.18 base/read/mem/Inner#file..entriesrust-analyzer cargo async_zip 0.0.18 file/ZipFile#entries..getrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]get().(index).ok_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]ok_or().(ZipErrorrust-analyzer cargo async_zip 0.0.18 error/ZipError#::EntryIndexOutOfBoundsrust-analyzer cargo async_zip 0.0.18 error/ZipError#EntryIndexOutOfBounds#)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
135 let mut cursor = Cursorrust-analyzer cargo futures-lite 2.1.0 io/Cursor#::newrust-analyzer cargo futures-lite 2.1.0 io/impl#[`Cursor<T>`]new().(&self.innerrust-analyzer cargo async_zip 0.0.18 base/read/mem/ZipFileReader#inner..datarust-analyzer cargo async_zip 0.0.18 base/read/mem/Inner#data.[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().);
136
137 stored_entry.seek_to_data_offsetrust-analyzer cargo async_zip 0.0.18 entry/impl#[StoredZipEntry]seek_to_data_offset().(&mut cursor).rust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().awaitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
138
139 let reader = ZipEntryReaderrust-analyzer cargo async_zip 0.0.18 base/read/io/entry/ZipEntryReader#::new_with_ownedrust-analyzer cargo async_zip 0.0.18 base/read/io/entry/impl#[`ZipEntryReader<'a, R, WithoutEntry>`]new_with_owned().(
140 cursor,
141 stored_entry.entryrust-analyzer cargo async_zip 0.0.18 entry/StoredZipEntry#entry..compressionrust-analyzer cargo async_zip 0.0.18 entry/impl#[ZipEntry]compression().(),
142 stored_entry.entryrust-analyzer cargo async_zip 0.0.18 entry/StoredZipEntry#entry..compressed_sizerust-analyzer cargo async_zip 0.0.18 entry/impl#[ZipEntry]compressed_size().(),
143 );
144
145 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(reader.into_with_entryrust-analyzer cargo async_zip 0.0.18 base/read/io/entry/impl#[`ZipEntryReader<'a, R, WithoutEntry>`]into_with_entry().(stored_entry))
146 }
147 }