Skip to content

Commit 0d3315f

Browse files
committed
Explicitly bind lifetimes for NlaBuffer.value()
Without this, the following results in an error: ```rust fn subnlas_by_kind<'a>(nlas: &'a [u8]) -> HashMap<u16, Vec<&'a [u8]>> { let mut out = HashMap::new(); for n in NlasIterator::new(nlas) { let n = n.unwrap(); let entry: &mut Vec<_> = out.entry(n.kind()).or_default(); (*entry).push(n.value()) } out } ``` ``` error[E0515]: cannot return value referencing local variable `n` | 212 | (*entry).push(n.value()) | - `n` is borrowed here 213 | } 214 | out | ^^^ returns a value referencing data owned by the current function ```
1 parent b40f3d6 commit 0d3315f

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

src/nla.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,9 @@ impl<T: AsRef<[u8]> + AsMut<[u8]>> NlaBuffer<T> {
148148
}
149149
}
150150

151-
impl<T: AsRef<[u8]> + ?Sized> NlaBuffer<&T> {
151+
impl<'buf: 'val, 'val, T: AsRef<[u8]> + ?Sized> NlaBuffer<&'buf T> {
152152
/// Return the `value` field
153-
pub fn value(&self) -> &[u8] {
153+
pub fn value(&self) -> &'val [u8] {
154154
&self.buffer.as_ref()[VALUE(self.value_length())]
155155
}
156156
}

0 commit comments

Comments
 (0)