rusticata / der-parser

BER/DER parser written in pure Rust. Fast, zero-copy, safe.
Apache License 2.0
85 stars 28 forks source link

loosen some lifetime restrictions #61

Closed ericmarkmartin closed 2 years ago

ericmarkmartin commented 2 years ago

I think annotating these self references is overly restrictive. Suppose I am parsing a &'static [u8] and call (e.g.) as_tagged within a function. Then 'a will get narrowed to the lifetime of the current function. This makes it impossible to return anything borrowed from the return of as_tagged from the function.

More concretely, the following compiles after merging this PR...

fn foo(i: &'static [u8]) -> der_parser::error::BerResult<&'static [u8]> {
    let (rem, obj) = der_parser::parse_ber(i)?;
    Ok((rem, obj.as_tagged()?.2.as_slice()?))
}

...but fails to compile under master...

error[E0515]: cannot return value referencing local variable `obj`
   --> src/foo.rs:3:5
    |
    |     Ok((rem, obj.as_tagged()?.2.as_slice()?))
    |     ^^^^^^^^^---^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |     |        |
    |     |        `obj` is borrowed here
    |     returns a value referencing data owned by the current function
chifflier commented 2 years ago

Thanks for your contribution!