electrum_client_netagnostic/
batch.rs

1//! Batch utilities
2//!
3//! This module contains definitions and helper functions used when making batch calls.
4
5use crate::types::{Call, Param};
6
7/// Helper structure that caches all the requests before they are actually sent to the server.
8///
9/// Calls on this function are stored and run when [`batch_call`](../client/struct.Client.html#method.batch_call)
10/// is run on a [`Client`](../client/struct.Client.html).
11///
12/// This structure can be used to make multiple *different* calls in one single run. For batch
13/// calls of the same type, there are shorthands methods defined on the
14/// [`Client`](../client/struct.Client.html), like
15/// [`batch_script_get_balance`](../client/struct.Client.html#method.batch_script_get_balance) to ask the
16/// server for the balance of multiple scripts with a single request.
17#[derive(Default)]
18pub struct Batch {
19    calls: Vec<Call>,
20}
21
22impl Batch {
23    /// Add a raw request to the batch queue
24    pub fn raw(&mut self, method: String, params: Vec<Param>) {
25        self.calls.push((method, params));
26    }
27
28    /// Returns an iterator on the batch
29    pub fn iter(&self) -> BatchIter {
30        BatchIter {
31            batch: self,
32            index: 0,
33        }
34    }
35}
36
37impl std::iter::IntoIterator for Batch {
38    type Item = (String, Vec<Param>);
39    type IntoIter = std::vec::IntoIter<Self::Item>;
40
41    fn into_iter(self) -> Self::IntoIter {
42        self.calls.into_iter()
43    }
44}
45
46pub struct BatchIter<'a> {
47    batch: &'a Batch,
48    index: usize,
49}
50
51impl<'a> std::iter::Iterator for BatchIter<'a> {
52    type Item = &'a (String, Vec<Param>);
53
54    fn next(&mut self) -> Option<Self::Item> {
55        let val = self.batch.calls.get(self.index);
56        self.index += 1;
57        val
58    }
59}