Merge rust-bitcoin/rust-bitcoin#3420: Refactor Arbitrary for Sequence

d39334e651 Refactor Arbitrary for Sequence (yancy)

Pull request description:

  Personally, I think this is a bit nicer and easier to reason about.

ACKs for top commit:
  tcharding:
    ACK d39334e651
  apoelstra:
    ACK d39334e651 successfully ran local tests

Tree-SHA512: 9816e81873c7c052e7204877f3cfc605f20feb399575af4c4527899bbdfe20031dd06d681539fe21226b4926891d1a69c5c3299976e553c7c75878e07f5ed068
This commit is contained in:
merge-script 2024-09-28 01:37:40 +00:00
commit 8740f0e43f
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
1 changed files with 18 additions and 10 deletions

View File

@ -242,29 +242,37 @@ impl fmt::Debug for Sequence {
units::impl_parse_str_from_int_infallible!(Sequence, u32, from_consensus); units::impl_parse_str_from_int_infallible!(Sequence, u32, from_consensus);
#[cfg(feature = "arbitrary")] #[cfg(feature = "arbitrary")]
#[cfg(feature = "alloc")]
impl<'a> Arbitrary<'a> for Sequence { impl<'a> Arbitrary<'a> for Sequence {
fn arbitrary(u: &mut Unstructured<'a>) -> arbitrary::Result<Self> { fn arbitrary(u: &mut Unstructured<'a>) -> arbitrary::Result<Self> {
let mut choice_range = 4;
if cfg!(feature = "alloc") {
choice_range = 8;
}
// Equally weight the cases of meaningful sequence numbers // Equally weight the cases of meaningful sequence numbers
let choice = u.int_in_range(0..=choice_range)?; let choice = u.int_in_range(0..=8)?;
match choice { match choice {
0 => Ok(Sequence::MAX), 0 => Ok(Sequence::MAX),
1 => Ok(Sequence::ZERO), 1 => Ok(Sequence::ZERO),
2 => Ok(Sequence::MIN_NO_RBF), 2 => Ok(Sequence::MIN_NO_RBF),
3 => Ok(Sequence::ENABLE_RBF_NO_LOCKTIME), 3 => Ok(Sequence::ENABLE_RBF_NO_LOCKTIME),
#[cfg(feature = "alloc")]
4 => Ok(Sequence::from_consensus(relative::Height::MIN.to_consensus_u32())), 4 => Ok(Sequence::from_consensus(relative::Height::MIN.to_consensus_u32())),
#[cfg(feature = "alloc")]
5 => Ok(Sequence::from_consensus(relative::Height::MAX.to_consensus_u32())), 5 => Ok(Sequence::from_consensus(relative::Height::MAX.to_consensus_u32())),
#[cfg(feature = "alloc")]
6 => Ok(Sequence::from_consensus(relative::Time::MIN.to_consensus_u32())), 6 => Ok(Sequence::from_consensus(relative::Time::MIN.to_consensus_u32())),
#[cfg(feature = "alloc")]
7 => Ok(Sequence::from_consensus(relative::Time::MAX.to_consensus_u32())), 7 => Ok(Sequence::from_consensus(relative::Time::MAX.to_consensus_u32())),
_ => Ok(Sequence(u.arbitrary()?)) _ => Ok(Sequence(u.arbitrary()?))
} }
} }
} }
#[cfg(feature = "arbitrary")]
#[cfg(not(feature = "alloc"))]
impl<'a> Arbitrary<'a> for Sequence {
fn arbitrary(u: &mut Unstructured<'a>) -> arbitrary::Result<Self> {
// Equally weight the cases of meaningful sequence numbers
let choice = u.int_in_range(0..=4)?;
match choice {
0 => Ok(Sequence::MAX),
1 => Ok(Sequence::ZERO),
2 => Ok(Sequence::MIN_NO_RBF),
3 => Ok(Sequence::ENABLE_RBF_NO_LOCKTIME),
_ => Ok(Sequence(u.arbitrary()?))
}
}
}