Skip to content

Commit 744528f

Browse files
committed
Explicitly bind lifetimes for NlaBuffer.value()
1 parent b40f3d6 commit 744528f

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

src/nla.rs

Lines changed: 12 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
}
@@ -374,4 +374,14 @@ mod tests {
374374
fn test_align_overflow() {
375375
assert_eq!(nla_align!(get_len() - 3), usize::MAX);
376376
}
377+
378+
// compile-time test, should not result in compiler complaints regarding
379+
// lifetimes or returning values allegedly owned by this function
380+
#[allow(dead_code)]
381+
fn nla_buffer_outlives_value(nlas: &[u8]) -> Option<&[u8]> {
382+
for nla in NlasIterator::new(nlas) {
383+
return Some(nla.unwrap().value())
384+
}
385+
None
386+
}
377387
}

0 commit comments

Comments
 (0)