Merge rust-bitcoin/rust-bitcoin#1183: Use `Borrow` trait bound for bip158 `Iterator::Item`

d8116de56a Use Borrow trait bound for bip158 Iterator::Item (Tobin C. Harding)

Pull request description:

  Currently the generic iterators in `bip158` return items of type slice reference. We can make the code cleaner (no explicit lifetimes) and more general if the generic iterator argument iterates over `Borrow<[u8]>`

ACKs for top commit:
  Kixunil:
    ACK d8116de56a
  apoelstra:
    ACK d8116de56a

Tree-SHA512: ce8111130d70a8c6f8d97f2c3f6c07bffd534461fdb599185129fc3cc4fc6c209e3187444082e47b7b55e4ec71fd8652fb2b86d2037573b99560ffd094175dc1
This commit is contained in:
Andrew Poelstra 2022-09-12 20:47:10 +00:00
commit 29bbe4042f
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
2 changed files with 22 additions and 16 deletions

View File

@ -139,18 +139,20 @@ impl BlockFilter {
} }
/// Returns true if any query matches against this [`BlockFilter`]. /// Returns true if any query matches against this [`BlockFilter`].
pub fn match_any<'a, I>(&self, block_hash: &BlockHash, query: I) -> Result<bool, Error> pub fn match_any<I>(&self, block_hash: &BlockHash, query: I) -> Result<bool, Error>
where where
I: Iterator<Item = &'a [u8]>, I: Iterator,
I::Item: Borrow<[u8]>,
{ {
let filter_reader = BlockFilterReader::new(block_hash); let filter_reader = BlockFilterReader::new(block_hash);
filter_reader.match_any(&mut self.content.as_slice(), query) filter_reader.match_any(&mut self.content.as_slice(), query)
} }
/// Returns true if all queries match against this [`BlockFilter`]. /// Returns true if all queries match against this [`BlockFilter`].
pub fn match_all<'a, I>(&self, block_hash: &BlockHash, query: I) -> Result<bool, Error> pub fn match_all<I>(&self, block_hash: &BlockHash, query: I) -> Result<bool, Error>
where where
I: Iterator<Item = &'a [u8]>, I: Iterator,
I::Item: Borrow<[u8]>,
{ {
let filter_reader = BlockFilterReader::new(block_hash); let filter_reader = BlockFilterReader::new(block_hash);
filter_reader.match_all(&mut self.content.as_slice(), query) filter_reader.match_all(&mut self.content.as_slice(), query)
@ -227,18 +229,20 @@ impl BlockFilterReader {
} }
/// Returns true if any query matches against this [`BlockFilterReader`]. /// Returns true if any query matches against this [`BlockFilterReader`].
pub fn match_any<'a, I, R>(&self, reader: &mut R, query: I) -> Result<bool, Error> pub fn match_any<I, R>(&self, reader: &mut R, query: I) -> Result<bool, Error>
where where
I: Iterator<Item = &'a [u8]>, I: Iterator,
I::Item: Borrow<[u8]>,
R: io::Read + ?Sized, R: io::Read + ?Sized,
{ {
self.reader.match_any(reader, query) self.reader.match_any(reader, query)
} }
/// Returns true if all queries match against this [`BlockFilterReader`]. /// Returns true if all queries match against this [`BlockFilterReader`].
pub fn match_all<'a, I, R>(&self, reader: &mut R, query: I) -> Result<bool, Error> pub fn match_all<I, R>(&self, reader: &mut R, query: I) -> Result<bool, Error>
where where
I: Iterator<Item = &'a [u8]>, I: Iterator,
I::Item: Borrow<[u8]>,
R: io::Read + ?Sized, R: io::Read + ?Sized,
{ {
self.reader.match_all(reader, query) self.reader.match_all(reader, query)
@ -258,9 +262,10 @@ impl GcsFilterReader {
} }
/// Returns true if any query matches against this [`GcsFilterReader`]. /// Returns true if any query matches against this [`GcsFilterReader`].
pub fn match_any<'a, I, R>(&self, reader: &mut R, query: I) -> Result<bool, Error> pub fn match_any<I, R>(&self, reader: &mut R, query: I) -> Result<bool, Error>
where where
I: Iterator<Item = &'a [u8]>, I: Iterator,
I::Item: Borrow<[u8]>,
R: io::Read + ?Sized, R: io::Read + ?Sized,
{ {
let mut decoder = reader; let mut decoder = reader;
@ -268,7 +273,7 @@ impl GcsFilterReader {
let reader = &mut decoder; let reader = &mut decoder;
// map hashes to [0, n_elements << grp] // map hashes to [0, n_elements << grp]
let nm = n_elements.0 * self.m; let nm = n_elements.0 * self.m;
let mut mapped = query.map(|e| map_to_range(self.filter.hash(e), nm)).collect::<Vec<_>>(); let mut mapped = query.map(|e| map_to_range(self.filter.hash(e.borrow()), nm)).collect::<Vec<_>>();
// sort // sort
mapped.sort_unstable(); mapped.sort_unstable();
if mapped.is_empty() { if mapped.is_empty() {
@ -301,9 +306,10 @@ impl GcsFilterReader {
} }
/// Returns true if all queries match against this [`GcsFilterReader`]. /// Returns true if all queries match against this [`GcsFilterReader`].
pub fn match_all<'a, I, R>(&self, reader: &mut R, query: I) -> Result<bool, Error> pub fn match_all<I, R>(&self, reader: &mut R, query: I) -> Result<bool, Error>
where where
I: Iterator<Item = &'a [u8]>, I: Iterator,
I::Item: Borrow<[u8]>,
R: io::Read + ?Sized, R: io::Read + ?Sized,
{ {
let mut decoder = reader; let mut decoder = reader;
@ -311,7 +317,7 @@ impl GcsFilterReader {
let reader = &mut decoder; let reader = &mut decoder;
// map hashes to [0, n_elements << grp] // map hashes to [0, n_elements << grp]
let nm = n_elements.0 * self.m; let nm = n_elements.0 * self.m;
let mut mapped = query.map(|e| map_to_range(self.filter.hash(e), nm)).collect::<Vec<_>>(); let mut mapped = query.map(|e| map_to_range(self.filter.hash(e.borrow()), nm)).collect::<Vec<_>>();
// sort // sort
mapped.sort_unstable(); mapped.sort_unstable();
mapped.dedup(); mapped.dedup();

View File

@ -139,10 +139,10 @@ mod io_extras {
#[rustfmt::skip] #[rustfmt::skip]
mod prelude { mod prelude {
#[cfg(all(not(feature = "std"), not(test)))] #[cfg(all(not(feature = "std"), not(test)))]
pub use alloc::{string::{String, ToString}, vec::Vec, boxed::Box, borrow::{Cow, ToOwned}, slice, rc, sync}; pub use alloc::{string::{String, ToString}, vec::Vec, boxed::Box, borrow::{Borrow, Cow, ToOwned}, slice, rc, sync};
#[cfg(any(feature = "std", test))] #[cfg(any(feature = "std", test))]
pub use std::{string::{String, ToString}, vec::Vec, boxed::Box, borrow::{Cow, ToOwned}, slice, rc, sync}; pub use std::{string::{String, ToString}, vec::Vec, boxed::Box, borrow::{Borrow, Cow, ToOwned}, slice, rc, sync};
#[cfg(all(not(feature = "std"), not(test)))] #[cfg(all(not(feature = "std"), not(test)))]
pub use alloc::collections::{BTreeMap, BTreeSet, btree_map, BinaryHeap}; pub use alloc::collections::{BTreeMap, BTreeSet, btree_map, BinaryHeap};