Skip to content

Commit 87c4703

Browse files
committed
Cleanup after Instructions refactoring
* Changes `Option` to `Result` to avoid repeated `.ok_or(...)` * Renames `max` to `min_push_len` * Removes useless variable
1 parent 2d75d84 commit 87c4703

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

src/blockdata/script.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -743,18 +743,18 @@ impl<'a> Instructions<'a> {
743743
/// takes `len` bytes long slice from iterator and returns it advancing iterator
744744
/// if the iterator is not long enough `None` is returned and the iterator is killed
745745
/// to avoid returning an infinite stream of errors.
746-
fn take_slice_or_kill(&mut self, len: usize) -> Option<&'a [u8]> {
746+
fn take_slice_or_kill(&mut self, len: usize) -> Result<&'a [u8], Error> {
747747
if self.data.len() >= len {
748748
let slice = &self.data.as_slice()[..len];
749749
self.data.nth(len.max(1) - 1);
750-
Some(slice)
750+
Ok(slice)
751751
} else {
752752
self.kill();
753-
None
753+
Err(Error::EarlyEndOfScript)
754754
}
755755
}
756756

757-
fn next_push_data_len(&mut self, len: usize, max: usize) -> Option<Result<Instruction<'a>, Error>> {
757+
fn next_push_data_len(&mut self, len: usize, min_push_len: usize) -> Option<Result<Instruction<'a>, Error>> {
758758
let n = match read_uint_iter(&mut self.data, len) {
759759
Ok(n) => n,
760760
// We do exhaustive matching to not forget to handle new variants if we extend
@@ -766,11 +766,11 @@ impl<'a> Instructions<'a> {
766766
return Some(Err(Error::EarlyEndOfScript));
767767
},
768768
};
769-
if self.enforce_minimal && n < max {
769+
if self.enforce_minimal && n < min_push_len {
770770
self.kill();
771771
return Some(Err(Error::NonMinimalPush));
772772
}
773-
Some(self.take_slice_or_kill(n).map(Instruction::PushBytes).ok_or(Error::EarlyEndOfScript))
773+
Some(self.take_slice_or_kill(n).map(Instruction::PushBytes))
774774
}
775775
}
776776

@@ -801,7 +801,7 @@ impl<'a> Iterator for Instructions<'a> {
801801
Some(Ok(Instruction::PushBytes(&[])))
802802
},
803803
_ => {
804-
Some(self.take_slice_or_kill(n).map(Instruction::PushBytes).ok_or(Error::EarlyEndOfScript))
804+
Some(self.take_slice_or_kill(n).map(Instruction::PushBytes))
805805
}
806806
}
807807
}
@@ -816,8 +816,7 @@ impl<'a> Iterator for Instructions<'a> {
816816
}
817817
// Everything else we can push right through
818818
_ => {
819-
let ret = Some(Ok(Instruction::Op(opcodes::All::from(byte))));
820-
ret
819+
Some(Ok(Instruction::Op(opcodes::All::from(byte))))
821820
}
822821
}
823822
}

0 commit comments

Comments
 (0)