bitcoin_explorer/iter/
iter_block.rs

1//!
2//! View development note of iter_connected.rs for implementation
3//! details of iter_block.rs, which follows similar principles.
4//!
5use crate::api::BitcoinDB;
6use bitcoin::Block;
7use par_iter_sync::{IntoParallelIteratorSync, ParIterSync};
8
9pub struct BlockIter<TBlock>(ParIterSync<TBlock>);
10
11impl<TBlock> BlockIter<TBlock>
12where
13    TBlock: From<Block> + Send + 'static,
14{
15    /// the worker threads are dispatched in this `new` constructor!
16    pub fn new<T>(db: &BitcoinDB, heights: T) -> Self
17    where
18        T: IntoIterator<Item = usize> + Send + 'static,
19        <T as IntoIterator>::IntoIter: Send + 'static,
20    {
21        let db_ref = db.clone();
22        BlockIter(
23            heights.into_par_iter_sync(move |h| match db_ref.get_block::<TBlock>(h) {
24                Ok(blk) => Ok(blk),
25                Err(_) => Err(()),
26            }),
27        )
28    }
29
30    /// the worker threads are dispatched in this `new` constructor!
31    pub fn from_range(db: &BitcoinDB, start: usize, end: usize) -> Self {
32        if end <= start {
33            BlockIter::new(db, Vec::new())
34        } else {
35            BlockIter::new(db, start..end)
36        }
37    }
38}
39
40impl<TBlock> Iterator for BlockIter<TBlock> {
41    type Item = TBlock;
42
43    fn next(&mut self) -> Option<Self::Item> {
44        self.0.next()
45    }
46}