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)]
21pub mod test_logger;
22#[allow(missing_docs)]
23#[cfg(any(test, feature = "test_utils"))]
24#[macro_use]
25pub mod test_utils;
26#[cfg(test)]
27pub(crate) mod mocks;
28#[allow(missing_docs)]
30pub mod ser_util;
31pub mod status;
33pub mod transaction_utils;
35pub mod velocity;
37
38pub use hex;
40
41pub const INITIAL_COMMITMENT_NUMBER: u64 = (1 << 48) - 1;
43
44use crate::prelude::*;
45use core::slice::Iter;
46use itertools::{put_back, PutBack};
47
48pub struct AddedItemsIter<'a, T: Ord + Eq> {
50 from: PutBack<Iter<'a, T>>,
51 to: PutBack<Iter<'a, T>>,
52}
53
54impl<'a, T: Ord + Eq> AddedItemsIter<'a, T> {
55 pub fn new(from: &'a Vec<T>, to: &'a Vec<T>) -> Self {
57 AddedItemsIter { from: put_back(from.iter()), to: put_back(to.iter()) }
58 }
59}
60
61impl<'a, T: Ord + Eq> Iterator for AddedItemsIter<'a, T> {
62 type Item = &'a T;
63
64 fn next(&mut self) -> Option<Self::Item> {
65 loop {
66 match self.from.next() {
67 None => return self.to.next(),
69 Some(next_from) => {
70 match self.to.next() {
71 None => return None,
73 Some(next_to) => {
74 if next_from < next_to {
75 self.to.put_back(next_to);
77 continue;
78 } else if next_from == next_to {
79 continue;
81 } else {
82 self.from.put_back(next_from);
84 return Some(next_to);
85 }
86 }
87 }
88 }
89 }
90 }
91 }
92}
93
94#[cfg(test)]
95mod tests {
96 use crate::util::AddedItemsIter;
97
98 #[test]
99 fn delta_test() {
100 fn check(from: Vec<u8>, to: Vec<u8>, expect: Vec<u8>) {
101 assert_eq!(AddedItemsIter::new(&from, &to).cloned().collect::<Vec<u8>>(), expect);
102 }
103
104 check(vec![], vec![1, 2, 4], vec![1, 2, 4]);
105 check(vec![3], vec![1, 2, 4], vec![1, 2, 4]);
106 check(vec![2, 3], vec![1, 2, 4], vec![1, 4]);
107 check(vec![1, 2, 3], vec![1, 2, 4], vec![4]);
108 check(vec![0, 1, 2, 3], vec![1, 2, 4], vec![4]);
109 check(vec![0, 1, 3], vec![1, 2, 4], vec![2, 4]);
110 check(vec![0, 1, 3], vec![1, 2, 4, 5], vec![2, 4, 5]);
111 }
112}