3Hren / msgpack-rust

MessagePack implementation for Rust / msgpack.org[Rust]
MIT License
1.17k stars 130 forks source link

qualify lifetime on `Utf8ResultRef::as_bytes` result #335

Open froydnj opened 1 year ago

froydnj commented 1 year ago

Without this, you can't write something like:

fn read_slice_ref<'a, R: value_ref::BorrowRead<'a>>(r: &mut R) -> Option<&'a [u8]> {
    match value_ref::read_value_ref(r).ok()? {
        rmpv::ValueRef::Binary(v) => Some(v),
        rmpv::ValueRef::String(u) => Some(u.as_bytes()),
        _ => None,
    }
}

because the compiler (version 1.72) complains:

error[E0515]: cannot return value referencing local variable `u`
   --> src/main.rs:124:38
    |
124 |         rmpv::ValueRef::String(u) => Some(u.as_bytes()),
    |                                      ^^^^^------------^
    |                                      |    |
    |                                      |    `u` is borrowed here
    |                                      returns a value referencing data owned by the current function

With this PR, things work as one would expect.