lightning_signer/util/
mod.rs1pub mod byte_utils;
3pub mod clock;
5pub mod crypto_utils;
7pub mod invoice_utils;
9#[macro_use]
11#[allow(unused_macros)]
12pub mod macro_logger;
13#[macro_use]
14pub mod debug_utils;
16pub mod log_utils;
18pub mod loopback;
20#[allow(missing_docs)]
21#[cfg(any(test, feature = "test_utils"))]
22#[macro_use]
23pub mod test_utils;
24#[cfg(test)]
25pub(crate) mod mocks;
26#[allow(missing_docs)]
28pub mod ser_util;
29pub mod status;
31pub mod transaction_utils;
33pub mod velocity;
35
36pub use hex;
38
39pub const INITIAL_COMMITMENT_NUMBER: u64 = (1 << 48) - 1;
41
42use crate::prelude::*;
43use core::slice::Iter;
44use itertools::{put_back, PutBack};
45
46pub struct AddedItemsIter<'a, T: Ord + Eq> {
48 from: PutBack<Iter<'a, T>>,
49 to: PutBack<Iter<'a, T>>,
50}
51
52impl<'a, T: Ord + Eq> AddedItemsIter<'a, T> {
53 pub fn new(from: &'a Vec<T>, to: &'a Vec<T>) -> Self {
55 AddedItemsIter { from: put_back(from.iter()), to: put_back(to.iter()) }
56 }
57}
58
59impl<'a, T: Ord + Eq> Iterator for AddedItemsIter<'a, T> {
60 type Item = &'a T;
61
62 fn next(&mut self) -> Option<Self::Item> {
63 loop {
64 match self.from.next() {
65 None => return self.to.next(),
67 Some(next_from) => {
68 match self.to.next() {
69 None => return None,
71 Some(next_to) => {
72 if next_from < next_to {
73 self.to.put_back(next_to);
75 continue;
76 } else if next_from == next_to {
77 continue;
79 } else {
80 self.from.put_back(next_from);
82 return Some(next_to);
83 }
84 }
85 }
86 }
87 }
88 }
89 }
90}
91
92#[cfg(test)]
93mod tests {
94 use crate::util::AddedItemsIter;
95
96 #[test]
97 fn delta_test() {
98 fn check(from: Vec<u8>, to: Vec<u8>, expect: Vec<u8>) {
99 assert_eq!(AddedItemsIter::new(&from, &to).cloned().collect::<Vec<u8>>(), expect);
100 }
101
102 check(vec![], vec![1, 2, 4], vec![1, 2, 4]);
103 check(vec![3], vec![1, 2, 4], vec![1, 2, 4]);
104 check(vec![2, 3], vec![1, 2, 4], vec![1, 4]);
105 check(vec![1, 2, 3], vec![1, 2, 4], vec![4]);
106 check(vec![0, 1, 2, 3], vec![1, 2, 4], vec![4]);
107 check(vec![0, 1, 3], vec![1, 2, 4], vec![2, 4]);
108 check(vec![0, 1, 3], vec![1, 2, 4, 5], vec![2, 4, 5]);
109 }
110}