Skip to content

Conversation

froydnj
Copy link

@froydnj froydnj commented Sep 7, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant