msgpack-rust icon indicating copy to clipboard operation
msgpack-rust copied to clipboard

qualify lifetime on `Utf8ResultRef::as_bytes` result

Open froydnj opened this issue 11 months ago • 0 comments

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.

froydnj avatar Sep 07 '23 20:09 froydnj