Debian Codegraph @

src/tokio/read/fs.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 a file system path.
5 //!
6 //! Concurrency is achieved as a result of:
7 //! - Wrapping the provided path within an [`Arc`] to allow shared ownership.
8 //! - Constructing a new [`File`] from the path when reading.
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::tokio::read::fs::ZipFileReader;
19 //! # use async_zip::error::Result;
20 //! # use futures_lite::io::AsyncReadExt;
21 //! #
22 //! async fn run() -> Result<()> {
23 //! let reader = ZipFileReader::new("./foo.zip").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::tokio::read::fs::ZipFileReader;
45 //! # use async_zip::error::Result;
46 //! # use futures_lite::io::AsyncReadExt;
47 //! #
48 //! async fn run() -> Result<()> {
49 //! let reader = ZipFileReader::new("./foo.zip").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/::{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#, 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/::pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/::{Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#, PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#};
78 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#;
79
80 use tokiorust-analyzer cargo tokio 1.25.0 crate/::fsrust-analyzer cargo tokio 1.25.0 fs/::Filerust-analyzer cargo tokio 1.25.0 fs/file/File#;
81 use tokiorust-analyzer cargo tokio 1.25.0 crate/::iorust-analyzer cargo tokio 1.25.0 io/::BufReaderrust-analyzer cargo tokio 1.25.0 io/util/buf_reader/BufReader#;
82 use tokio_utilrust-analyzer cargo tokio-util 0.7.4 crate/::compatrust-analyzer cargo tokio-util 0.7.4 compat/::{Compatrust-analyzer cargo tokio-util 0.7.4 compat/Compat#, TokioAsyncReadCompatExtrust-analyzer cargo tokio-util 0.7.4 compat/TokioAsyncReadCompatExt#};
83
84 struct Innerrust-analyzer cargo async_zip 0.0.18 tokio/read/fs/Inner# {
85 pathrust-analyzer cargo async_zip 0.0.18 tokio/read/fs/Inner#path.: PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#,
86 filerust-analyzer cargo async_zip 0.0.18 tokio/read/fs/Inner#file.: ZipFilerust-analyzer cargo async_zip 0.0.18 file/ZipFile#,
87 }
88
89 /// A concurrent ZIP reader which acts over a file system path.
90 #[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:)]
91 pub struct ZipFileReaderrust-analyzer cargo async_zip 0.0.18 tokio/read/fs/ZipFileReader# {
92 innerrust-analyzer cargo async_zip 0.0.18 tokio/read/fs/ZipFileReader#inner.: Arcrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc sync/Arc#<Innerrust-analyzer cargo async_zip 0.0.18 tokio/read/fs/Inner#>,
93 }
94
95 impl ZipFileReaderrust-analyzer cargo async_zip 0.0.18 tokio/read/fs/ZipFileReader# {
96 /// Constructs a new ZIP reader from a file system path.
97 pub async fn newrust-analyzer cargo async_zip 0.0.18 tokio/read/fs/impl#[ZipFileReader]new().<P>(path: P) -> Resultrust-analyzer cargo async_zip 0.0.18 error/Result#<ZipFileReaderrust-analyzer cargo async_zip 0.0.18 tokio/read/fs/ZipFileReader#>
98 where
99 P: AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#>,
100 {
101 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().(Filerust-analyzer cargo tokio 1.25.0 fs/file/File#::openrust-analyzer cargo tokio 1.25.0 fs/file/impl#[File]open().(&path).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()..compatrust-analyzer cargo tokio-util 0.7.4 compat/TokioAsyncReadCompatExt#compat().()).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().;
102 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(ZipFileReaderrust-analyzer cargo async_zip 0.0.18 tokio/read/fs/ZipFileReader#::from_raw_partsrust-analyzer cargo async_zip 0.0.18 tokio/read/fs/impl#[ZipFileReader]from_raw_parts().(path, file))
103 }
104
105 /// Constructs a ZIP reader from a file system path and ZIP file information derived from that path.
106 ///
107 /// Providing a [`ZipFile`] that wasn't derived from that path may lead to inaccurate parsing.
108 pub fn from_raw_partsrust-analyzer cargo async_zip 0.0.18 tokio/read/fs/impl#[ZipFileReader]from_raw_parts().<P>(path: P, file: ZipFilerust-analyzer cargo async_zip 0.0.18 file/ZipFile#) -> ZipFileReaderrust-analyzer cargo async_zip 0.0.18 tokio/read/fs/ZipFileReader#
109 where
110 P: AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#>,
111 {
112 ZipFileReaderrust-analyzer cargo async_zip 0.0.18 tokio/read/fs/ZipFileReader# { innerrust-analyzer cargo async_zip 0.0.18 tokio/read/fs/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 tokio/read/fs/Inner# { pathrust-analyzer cargo async_zip 0.0.18 tokio/read/fs/Inner#path.: path.as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#as_ref().().to_ownedrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path][ToOwned]to_owned().(), filerust-analyzer cargo async_zip 0.0.18 tokio/read/fs/Inner#file. }) }
113 }
114
115 /// Returns this ZIP file's information.
116 pub fn filerust-analyzer cargo async_zip 0.0.18 tokio/read/fs/impl#[ZipFileReader]file().(&self) -> &ZipFilerust-analyzer cargo async_zip 0.0.18 file/ZipFile# {
117 &self.innerrust-analyzer cargo async_zip 0.0.18 tokio/read/fs/ZipFileReader#inner..filerust-analyzer cargo async_zip 0.0.18 tokio/read/fs/Inner#file.
118 }
119
120 /// Returns the file system path provided to the reader during construction.
121 pub fn pathrust-analyzer cargo async_zip 0.0.18 tokio/read/fs/impl#[ZipFileReader]path().(&self) -> &Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path# {
122 &self.innerrust-analyzer cargo async_zip 0.0.18 tokio/read/fs/ZipFileReader#inner..pathrust-analyzer cargo async_zip 0.0.18 tokio/read/fs/Inner#path.
123 }
124
125 /// Returns a new entry reader if the provided index is valid.
126 pub async fn reader_without_entryrust-analyzer cargo async_zip 0.0.18 tokio/read/fs/impl#[ZipFileReader]reader_without_entry().(
127 &self,
128 index: usize,
129 ) -> Resultrust-analyzer cargo async_zip 0.0.18 error/Result#<ZipEntryReaderrust-analyzer cargo async_zip 0.0.18 base/read/io/entry/ZipEntryReader#<'static, Compatrust-analyzer cargo tokio-util 0.7.4 compat/Compat#<BufReaderrust-analyzer cargo tokio 1.25.0 io/util/buf_reader/BufReader#<Filerust-analyzer cargo tokio 1.25.0 fs/file/File#>>, WithoutEntryrust-analyzer cargo async_zip 0.0.18 base/read/io/entry/WithoutEntry#>> {
130 let stored_entry = self.innerrust-analyzer cargo async_zip 0.0.18 tokio/read/fs/ZipFileReader#inner..filerust-analyzer cargo async_zip 0.0.18 tokio/read/fs/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().;
131 let mut fs_file = BufReaderrust-analyzer cargo tokio 1.25.0 io/util/buf_reader/BufReader#::newrust-analyzer cargo tokio 1.25.0 io/util/buf_reader/impl#[`BufReader<R>`]new().(Filerust-analyzer cargo tokio 1.25.0 fs/file/File#::openrust-analyzer cargo tokio 1.25.0 fs/file/impl#[File]open().(&self.innerrust-analyzer cargo async_zip 0.0.18 tokio/read/fs/ZipFileReader#inner..pathrust-analyzer cargo async_zip 0.0.18 tokio/read/fs/Inner#path.).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().).compatrust-analyzer cargo tokio-util 0.7.4 compat/TokioAsyncReadCompatExt#compat().();
132
133 stored_entry.seek_to_data_offsetrust-analyzer cargo async_zip 0.0.18 entry/impl#[StoredZipEntry]seek_to_data_offset().(&mut fs_file).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().;
134
135 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().(
136 fs_file,
137 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().(),
138 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().(),
139 ))
140 }
141
142 /// Returns a new entry reader if the provided index is valid.
143 pub async fn reader_with_entryrust-analyzer cargo async_zip 0.0.18 tokio/read/fs/impl#[ZipFileReader]reader_with_entry().(
144 &self,
145 index: usize,
146 ) -> Resultrust-analyzer cargo async_zip 0.0.18 error/Result#<ZipEntryReaderrust-analyzer cargo async_zip 0.0.18 base/read/io/entry/ZipEntryReader#<'_, Compatrust-analyzer cargo tokio-util 0.7.4 compat/Compat#<BufReaderrust-analyzer cargo tokio 1.25.0 io/util/buf_reader/BufReader#<Filerust-analyzer cargo tokio 1.25.0 fs/file/File#>>, WithEntryrust-analyzer cargo async_zip 0.0.18 base/read/io/entry/WithEntry#<'_>>> {
147 let stored_entry = self.innerrust-analyzer cargo async_zip 0.0.18 tokio/read/fs/ZipFileReader#inner..filerust-analyzer cargo async_zip 0.0.18 tokio/read/fs/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().;
148 let mut fs_file = BufReaderrust-analyzer cargo tokio 1.25.0 io/util/buf_reader/BufReader#::newrust-analyzer cargo tokio 1.25.0 io/util/buf_reader/impl#[`BufReader<R>`]new().(Filerust-analyzer cargo tokio 1.25.0 fs/file/File#::openrust-analyzer cargo tokio 1.25.0 fs/file/impl#[File]open().(&self.innerrust-analyzer cargo async_zip 0.0.18 tokio/read/fs/ZipFileReader#inner..pathrust-analyzer cargo async_zip 0.0.18 tokio/read/fs/Inner#path.).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().).compatrust-analyzer cargo tokio-util 0.7.4 compat/TokioAsyncReadCompatExt#compat().();
149
150 stored_entry.seek_to_data_offsetrust-analyzer cargo async_zip 0.0.18 entry/impl#[StoredZipEntry]seek_to_data_offset().(&mut fs_file).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().;
151
152 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().(
153 fs_file,
154 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().(),
155 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().(),
156 );
157
158 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))
159 }
160 }