Skip to content

Commit d36d580

Browse files
committed
well, separate function is not required when it comes to triggering lifetime-related compiler errors
1 parent ced8f3b commit d36d580

File tree

1 file changed

+25
-16
lines changed

1 file changed

+25
-16
lines changed

src/nla.rs

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -375,15 +375,6 @@ mod tests {
375375
assert_eq!(nla_align!(get_len() - 3), usize::MAX);
376376
}
377377

378-
// compile-time test: it should be possible to pass &[u8] through
379-
// NlasIterator and return one of its output &[u8]s without facing
380-
// compiler error about lifetimes and returning borrows from something
381-
// that this funciton owns
382-
fn last_nla_from_buffer(nlas: &[u8]) -> Option<Result<&[u8], DecodeError>> {
383-
NlasIterator::new(nlas).last()
384-
.map(|nla| nla.map(|nla| nla.value()))
385-
}
386-
387378
#[test]
388379
fn test_nlas_iterator() {
389380
// sample NFTA_LIST_ELEM from nftables, with nested nlas at the end
@@ -395,15 +386,33 @@ mod tests {
395386
0x1b, 0x0c, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x8e, 0x14,
396387
0x56, 0x39
397388
];
398-
let mut iter = NlasIterator::new(NESTED_NLAS);
399-
// DecodeError does not implement PartialEq, hence
400-
// unwrap() and is_none()
401-
assert_eq!(iter.next().unwrap().unwrap().value(), &NESTED_NLAS[4..12]);
402-
assert_eq!(iter.next().unwrap().unwrap().value(), &NESTED_NLAS[16..]);
403-
assert!(iter.next().is_none());
389+
390+
let last = {
391+
let mut iter = NlasIterator::new(NESTED_NLAS);
392+
393+
// DecodeError does not implement PartialEq, hence
394+
// unwrap() and is_none()
395+
396+
assert_eq!(
397+
iter.next().unwrap().unwrap().value(),
398+
&NESTED_NLAS[4..12]
399+
);
400+
401+
// it should be possible to pass &[u8] through
402+
// NlasIterator and return one of its output &[u8]s without facing
403+
// compiler error about lifetimes and returning borrows from
404+
// something that this funciton owns
405+
let last = iter.next().unwrap().unwrap().value();
406+
// asserted later
407+
408+
assert!(iter.next().is_none());
409+
410+
last
411+
};
412+
assert_eq!(last, &NESTED_NLAS[16..]);
404413

405414
// this sholud be an Err()
406415
let truncated = &NESTED_NLAS[ .. NESTED_NLAS.len()-1];
407-
assert!(last_nla_from_buffer(truncated).unwrap().is_err());
416+
assert!(NlasIterator::new(truncated).last().unwrap().is_err());
408417
}
409418
}

0 commit comments

Comments
 (0)