Merge rust-bitcoin/rust-bitcoin#3121: bip158: Improve casting

ea9f6e8f97 Cast after calling min (Tobin C. Harding)
342fe18ad0 Use From in map_to_range (Tobin C. Harding)
d9331794f1 bip158: Fix typo (Tobin C. Harding)

Pull request description:

  Patch 1 is a typo fix, other two are cast improvements. Done as part of #2941.

  Leaves a cast to `u64` in there, will be removed once we get to the `ToU64` stuff. One other remaining cast in the module is obviously bit twiddling.

ACKs for top commit:
  Kixunil:
    ACK ea9f6e8f97
  apoelstra:
    ACK ea9f6e8f97 successfully ran local tests

Tree-SHA512: 92fa6225621f6c1eea4864472a5b9c60cb244e62cdfbee4074067da43b8f2bd4e3ed1a249ba40b994cad824e97585f3140747b03677ef1596c80be3007c94a7c
This commit is contained in:
merge-script 2024-08-05 20:58:29 +00:00
commit 865fdf78dd
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
1 changed files with 5 additions and 5 deletions

View File

@ -362,7 +362,7 @@ impl GcsFilterReader {
}
/// Fast reduction of hash to [0, nm) range.
fn map_to_range(hash: u64, nm: u64) -> u64 { ((hash as u128 * nm as u128) >> 64) as u64 }
fn map_to_range(hash: u64, nm: u64) -> u64 { ((u128::from(hash) * u128::from(nm)) >> 64) as u64 }
/// Golomb-Rice encoded filter writer.
pub struct GcsFilterWriter<'a, W> {
@ -400,7 +400,7 @@ impl<'a, W: Write> GcsFilterWriter<'a, W> {
// write number of elements as varint
let mut wrote = VarInt::from(mapped.len()).consensus_encode(self.writer)?;
// write out deltas of sorted values into a Golonb-Rice coded bit stream
// write out deltas of sorted values into a Golomb-Rice coded bit stream
let mut writer = BitStreamWriter::new(self.writer);
let mut last = 0;
for data in mapped {
@ -435,9 +435,9 @@ impl GcsFilter {
let mut wrote = 0;
let mut q = n >> self.p;
while q > 0 {
let nbits = cmp::min(q, 64);
wrote += writer.write(!0u64, nbits as u8)?;
q -= nbits;
let nbits = cmp::min(q, 64) as u8; // cast ok, 64 fits into a `u8`
wrote += writer.write(!0u64, nbits)?;
q -= u64::from(nbits);
}
wrote += writer.write(0, 1)?;
wrote += writer.write(n, self.p)?;