Merge rust-bitcoin/rust-bitcoin#2035: Improve witness display

de95bf52cb Use checked_sub (Tobin C. Harding)

Pull request description:

  Recently we "if" guarded subtraction manually using `> 0`, we can better convey the meaning by using `checked_sub` and pattern match on the option.

  Refactor only, no logic changes.

ACKs for top commit:
  RCasatta:
    utACK de95bf52cb
  apoelstra:
    ACK de95bf52cb

Tree-SHA512: 2514cc2d8af89158e5e5e5a866f3fadb4927ba07dfb4e077fd16a98acf638588bee5ce03e2dc73fbda0b5064c30d8773d3be583c03c2a5336b8738c212a9776f
This commit is contained in:
Andrew Poelstra 2023-09-03 19:10:09 +00:00
commit 70223027b9
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
1 changed files with 21 additions and 17 deletions

View File

@ -68,27 +68,31 @@ fn fmt_debug(w: &Witness, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error>
f.write_str("witnesses: [")?;
let instructions = w.iter();
if instructions.len() > 0 {
let last_instruction = instructions.len() - 1;
match instructions.len().checked_sub(1) {
Some(last_instruction) => {
for (i, instruction) in instructions.enumerate() {
let bytes = instruction.iter();
if bytes.len() > 0 {
let last_byte = bytes.len() - 1;
match bytes.len().checked_sub(1) {
Some(last_byte) => {
f.write_str("[")?;
for (j, byte) in bytes.enumerate() {
write!(f, "{:#04x}", byte)?;
f.write_str(comma_or_close(j, last_byte))?;
}
} else {
}
None => {
// This is possible because the varint is not part of the instruction (see Iter).
write!(f, "[]")?;
}
}
f.write_str(comma_or_close(i, last_instruction))?;
}
} else {
}
None => {
// Witnesses can be empty because the 0x00 var int is not stored in content.
write!(f, "]")?;
}
}
f.write_str(" }")
}