Skip to content

Commit

Permalink
feat(chain): impl Append for Option<T>
Browse files Browse the repository at this point in the history
  • Loading branch information
notmandatory committed Dec 7, 2023
1 parent f108093 commit 80b8df8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
11 changes: 11 additions & 0 deletions crates/chain/src/tx_data_traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,17 @@ impl<T> Append for Vec<T> {
}
}

impl<T> Append for Option<T> {
// If other is Some then replace self's value with other's value, if other is None do nothing.
fn append(&mut self, other: Self) {
other.and_then(|v| self.replace(v));
}

fn is_empty(&self) -> bool {
self.is_none()
}
}

macro_rules! impl_append_for_tuple {
($($a:ident $b:tt)*) => {
impl<$($a),*> Append for ($($a,)*) where $($a: Append),* {
Expand Down
6 changes: 3 additions & 3 deletions crates/chain/src/tx_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ impl<A: Clone + Ord> TxGraph<A> {
let tx_entry = self
.txs
.entry(outpoint.txid)
.or_insert_with(Default::default);
.or_default();

match tx_entry {
(TxNodeInternal::Whole(_), _, _) => { /* do nothing since we already have full tx */
Expand All @@ -575,13 +575,13 @@ impl<A: Clone + Ord> TxGraph<A> {

for (anchor, txid) in changeset.anchors {
if self.anchors.insert((anchor.clone(), txid)) {
let (_, anchors, _) = self.txs.entry(txid).or_insert_with(Default::default);
let (_, anchors, _) = self.txs.entry(txid).or_default();
anchors.insert(anchor);
}
}

for (txid, new_last_seen) in changeset.last_seen {
let (_, _, last_seen) = self.txs.entry(txid).or_insert_with(Default::default);
let (_, _, last_seen) = self.txs.entry(txid).or_default();
if new_last_seen > *last_seen {
*last_seen = new_last_seen;
}
Expand Down

0 comments on commit 80b8df8

Please sign in to comment.