From 04a8f89f0566e902f7064f428ff8afc247c83ec2 Mon Sep 17 00:00:00 2001 From: Martin Habovstiak Date: Wed, 1 Dec 2021 21:25:17 +0100 Subject: [PATCH] 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. --- src/blockdata/script.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/blockdata/script.rs b/src/blockdata/script.rs index 23419eaa..b5ba2040 100644 --- a/src/blockdata/script.rs +++ b/src/blockdata/script.rs @@ -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()); + } }