[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>.
Cargo.toml:
main.rs:
Expected result: Code compiles.
Actual result:
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>
.