Implement `FusedIterator` for `Instructions`

`Instructions` guarantee to return `None` from empty iterator so we
should signal this in type system so that the code can be optimized
better. This also adds a test to make sure this property holds.
This commit is contained in:
Martin Habovstiak 2021-12-01 21:25:17 +01:00
parent 51b1abdab2
commit 04a8f89f05
1 changed files with 11 additions and 0 deletions

View File

@ -744,6 +744,8 @@ impl<'a> Iterator for Instructions<'a> {
}
}
impl<'a> ::core::iter::FusedIterator for Instructions<'a> {}
impl Builder {
/// Creates a new empty script
pub fn new() -> Self {
@ -1388,5 +1390,14 @@ mod test {
assert_eq!(script, ::bincode::deserialize(&bincode).unwrap());
}
#[test]
fn test_instructions_are_fused() {
let script = Script::new();
let mut instructions = script.instructions();
assert!(instructions.next().is_none());
assert!(instructions.next().is_none());
assert!(instructions.next().is_none());
assert!(instructions.next().is_none());
}
}