Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Writer::into_stream_writer() does not work with borrowed Write implementations #315

Open
ravenexp opened this issue Nov 28, 2021 · 1 comment

Comments

@ravenexp
Copy link

Sample code exhibiting the issue:

use std::io::Write;

fn encode() -> Vec<u8> {
    let mut pngbuf: Vec<u8> = Vec::new();

    {
        let mut encoder = png::Encoder::new(std::io::Cursor::new(&mut pngbuf), 1, 1);
        encoder.set_color(png::ColorType::Grayscale);
        encoder.set_depth(png::BitDepth::Eight);

        let mut writer = encoder
            .write_header()
            .unwrap()
            .into_stream_writer()
            .unwrap();

        writer.write_all(b"x").unwrap();
    }

    pngbuf
}

leads to the following error:

error[E0597]: `pngbuf` does not live long enough
  --> src/main.rs:7:66
   |
7  |         let mut encoder = png::Encoder::new(std::io::Cursor::new(&mut pngbuf), 1, 1);
   |                                             ---------------------^^^^^^^^^^^-
   |                                             |                    |
   |                                             |                    borrowed value does not live long enough
   |                                             argument requires that `pngbuf` is borrowed for `'static`
...
21 | }
   | - `pngbuf` dropped here while still borrowed

error[E0505]: cannot move out of `pngbuf` because it is borrowed
  --> src/main.rs:20:5
   |
7  |         let mut encoder = png::Encoder::new(std::io::Cursor::new(&mut pngbuf), 1, 1);
   |                                             ---------------------------------
   |                                             |                    |
   |                                             |                    borrow of `pngbuf` occurs here
   |                                             argument requires that `pngbuf` is borrowed for `'static`
...
20 |     pngbuf
   |     ^^^^^^ move out of `pngbuf` occurs here

The issue and the possible solutions were previously discussed in #298 (comment)

Since #298 is closed and this is a actually different issue introduced only in v0.17 I'm submitting a new one.

@fintelia
Copy link
Contributor

This version does work:

fn encode() -> Vec<u8> {
    let mut pngbuf: Vec<u8> = Vec::new();

    {
        let mut encoder = png::Encoder::new(std::io::Cursor::new(&mut pngbuf), 1, 1);
        encoder.set_color(png::ColorType::Grayscale);
        encoder.set_depth(png::BitDepth::Eight);

        let mut writer = encoder
            .write_header()
            .unwrap();

        let mut writer = writer
            .stream_writer()
            .unwrap();

        writer.write_all(b"x").unwrap();
    }

    pngbuf
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants