Use slice patterns

Some code looks better with slice patterns. This changes `bip32` to use
them.
This commit is contained in:
Martin Habovstiak 2023-04-02 00:55:22 +02:00
parent c7f970d8dc
commit 873501a85f
1 changed files with 13 additions and 17 deletions

View File

@ -607,14 +607,11 @@ impl ExtendedPrivKey {
return Err(Error::WrongExtendedKeyLength(data.len())); return Err(Error::WrongExtendedKeyLength(data.len()));
} }
let network = if data[0..4] == [0x04u8, 0x88, 0xAD, 0xE4] { let network = match data {
Network::Bitcoin [0x04u8, 0x88, 0xAD, 0xE4, ..] => Network::Bitcoin,
} else if data[0..4] == [0x04u8, 0x35, 0x83, 0x94] { [0x04u8, 0x35, 0x83, 0x94, ..] => Network::Testnet,
Network::Testnet [b0, b1, b2, b3, ..] => return Err(Error::UnknownVersion([*b0, *b1, *b2, *b3])),
} else { _ => unreachable!("length checked above"),
let mut ver = [0u8; 4];
ver.copy_from_slice(&data[0..4]);
return Err(Error::UnknownVersion(ver));
}; };
Ok(ExtendedPrivKey { Ok(ExtendedPrivKey {
@ -745,16 +742,15 @@ impl ExtendedPubKey {
return Err(Error::WrongExtendedKeyLength(data.len())); return Err(Error::WrongExtendedKeyLength(data.len()));
} }
let network = match data {
[0x04u8, 0x88, 0xB2, 0x1E, ..] => Network::Bitcoin,
[0x04u8, 0x35, 0x87, 0xCF, ..] => Network::Testnet,
[b0, b1, b2, b3, ..] => return Err(Error::UnknownVersion([*b0, *b1, *b2, *b3])),
_ => unreachable!("length checked above"),
};
Ok(ExtendedPubKey { Ok(ExtendedPubKey {
network: if data[0..4] == [0x04u8, 0x88, 0xB2, 0x1E] { network,
Network::Bitcoin
} else if data[0..4] == [0x04u8, 0x35, 0x87, 0xCF] {
Network::Testnet
} else {
let mut ver = [0u8; 4];
ver.copy_from_slice(&data[0..4]);
return Err(Error::UnknownVersion(ver));
},
depth: data[4], depth: data[4],
parent_fingerprint: data[5..9] parent_fingerprint: data[5..9]
.try_into() .try_into()