Majored / rs-async-zip

An asynchronous ZIP archive reading/writing crate.
MIT License
130 stars 44 forks source link

Can't properly use the stream method #143

Open TTheGlock opened 2 months ago

TTheGlock commented 2 months ago

I'm trying to create a program which will, with a given zip file, read and extract files in it using the stream method, and I get a few errors.

Working with v0.0.17

Part of code below

pub async fn unzip(zip_file: &Uri, dest: &Uri, session_dir: &Uri) -> OwnFrameworkResult<Vec<Uri>> {
    use async_zip::base::read::stream::ZipFileReader;

    let zip_bufreader: BufReader<Pin<Box<dyn AsyncRead + Send + Sync>>> = BufReader::new([...]) 

    let mut zip_filereader: ZipFileReader<Ready<BufReader<Pin<Box<dyn AsyncRead + Send + Sync>>>>> = ZipFileReader::new(zip_bufreader);

    while let Some(mut zip_entry) = zip_filereader.next_with_entry().await.unwrap() {
        let mut zip_filename: String = zip_entry.reader().entry().filename().as_str().unwrap().to_string();
        if !dest.get_name().ends_with("/") {
            zip_filename = format!("/{zip_filename}");
        }
        let zip_tmp_uri: Uri = dest.clone().join_path(&zip_filename);

        // writing files here using my own framework :
        // - to : &zip_tmp_uri
        // - from :  Box::pin(zip_content)

        let zip_content = zip_entry.reader_mut();

        zip_filereader = zip_entry.done().await.unwrap();
    }
}

I get these errors :

error[E0597]: `zip_entry` does not live long enough
   --> test/src/archive_stream.rs:115:27
    |
105 |     while let Some(mut zip_entry) = zip_filereader.next_with_entry()unwrap() {
    |                    ------------- binding `zip_entry` declared here
...
115 |         let zip_content = zip_entry.reader_mut();
    |                           ^^^^^^^^^ borrowed value does not live long enough
116 |         scheme
117 |             .write(&zip_tmp_uri, Box::pin(zip_content))
    |                                  --------------------- cast requires that `zip_entry` is borrowed for `'static`
...
139 |     }
    |     - `zip_entry` dropped here while still borrowed
error[E0505]: cannot move out of `zip_entry` because it is borrowed
   --> test/src/archive_stream.rs:138:26
    |
105 |     while let Some(mut zip_entry) = zip_filereader.next_with_entry().await.unwrap() {
    |                    ------------- binding `zip_entry` declared here
...
115 |         let zip_content = zip_entry.reader_mut();
    |                           --------- borrow of `zip_entry` occurs here
116 |         scheme
117 |             .write(&zip_tmp_uri, Box::pin(zip_content))
    |                                  --------------------- cast requires that `zip_entry` is borrowed for `'static`
...
138 |         zip_filereader = zip_entry.done().await.unwrap(); 
    |                          ^^^^^^^^^ move out of `zip_entry` occurs here

I was wondering if there was something blocking with next_with_entry(mut self), which does not take a reference for example ?

Majored commented 2 weeks ago

Sorry for the delay here. Is your scheme.write() framework method taking a trait object? If so, are you specifying a lifetime different from 'static?

For instance, these two examples modified from your code (take note of the 'a on the dyn of test()): Screenshot 2024-09-07 at 14 26 41 Screenshot 2024-09-07 at 14 26 12