image-rs / image-png

PNG decoding and encoding library in pure Rust
https://docs.rs/png
Apache License 2.0
347 stars 139 forks source link

How do I get an owned Info? #464

Closed AlyoshaVasilieva closed 5 months ago

AlyoshaVasilieva commented 5 months ago

Cargo.toml:

[package]
name = "repro"
version = "0.1.0"
edition = "2021"

[dependencies]
png = "0.17.11"

main.rs:

use std::fs::File;
use std::path::Path;

use png::{Decoder, DecodingError, Info};

fn main() {
    decode_and_return_info("irrelevant.png").unwrap();
}

fn decode_and_return_info<P: AsRef<Path>>(png: P) -> Result<Info<'static>, DecodingError> {
    let dec = Decoder::new(File::open(png.as_ref()).unwrap());
    let mut rdr = dec.read_info()?;
    rdr.finish()?;
    Ok(rdr.info().clone().to_owned())
}

Expected result: Code compiles.

Actual result:

error[E0515]: cannot return value referencing local variable `rdr`
  --> src\main.rs:14:5
   |
14 |     Ok(rdr.info().clone().to_owned())
   |     ^^^---^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |     |  |
   |     |  `rdr` is borrowed here
   |     returns a value referencing data owned by the current function

error: aborting due to previous error

I'm trying to read a PNG, make slight modifications, then save a new PNG, and ran into the above error when trying to put the decoding part into its own function. I can't find any way to get an Info<'static>.

fintelia commented 5 months ago

Seems we were returning the Info struct with the wrong lifetime parameter... just opened #465 to fix