Skip to content

Commit

Permalink
f avoid matches!
Browse files Browse the repository at this point in the history
  • Loading branch information
arik-so committed Jul 18, 2024
1 parent 43e6f89 commit 73a56e3
Showing 1 changed file with 45 additions and 39 deletions.
84 changes: 45 additions & 39 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,53 +320,59 @@ fn serialize_delta<L: Deref + Clone>(serialization_details: &SerializationSet, s
0: used for odd keys
*/

if matches!(strategy, NodeSerializationStrategy::Mutated(MutatedNodeProperties { addresses: true, .. }) | NodeSerializationStrategy::Full) {
let address_set = &node_delta.latest_details.as_ref().unwrap().addresses;
let mut address_serialization = Vec::new();
match strategy {
NodeSerializationStrategy::Mutated(MutatedNodeProperties { addresses: true, .. }) | NodeSerializationStrategy::Full => {
let address_set = &node_delta.latest_details.as_ref().unwrap().addresses;
let mut address_serialization = Vec::new();

// we don't know a priori how many are <= 255 bytes
let mut total_address_count = 0u8;

for address in address_set.iter() {
if total_address_count == u8::MAX {
// don't serialize more than 255 addresses
break;
}
if let Ok(serialized_length) = u8::try_from(address.serialized_length()) {
total_address_count += 1;
serialized_length.write(&mut address_serialization).unwrap();
address.write(&mut address_serialization).unwrap();
};
}

// we don't know a priori how many are <= 255 bytes
let mut total_address_count = 0u8;
node_address_update_count += 1;
node_has_update = true;

for address in address_set.iter() {
if total_address_count == u8::MAX {
// don't serialize more than 255 addresses
break;
}
if let Ok(serialized_length) = u8::try_from(address.serialized_length()) {
total_address_count += 1;
serialized_length.write(&mut address_serialization).unwrap();
address.write(&mut address_serialization).unwrap();
};
}

node_address_update_count += 1;
node_has_update = true;

// signal the presence of node addresses
current_node_delta_serialization[0] |= 1 << 2;
// serialize the actual addresses and count
total_address_count.write(&mut current_node_delta_serialization).unwrap();
current_node_delta_serialization.append(&mut address_serialization);
// signal the presence of node addresses
current_node_delta_serialization[0] |= 1 << 2;
// serialize the actual addresses and count
total_address_count.write(&mut current_node_delta_serialization).unwrap();
current_node_delta_serialization.append(&mut address_serialization);
},
_ => {}
}

if matches!(strategy, NodeSerializationStrategy::Mutated(MutatedNodeProperties { features: true, .. }) | NodeSerializationStrategy::Full) {
let latest_features = &node_delta.latest_details.as_ref().unwrap().features;
node_feature_update_count += 1;
node_has_update = true;

// are these features among the most common ones?
if let Some(index) = serialization_details.node_announcement_feature_defaults.iter().position(|f| f == latest_features) {
// this feature set is among the 6 defaults
current_node_delta_serialization[0] |= ((index + 1) as u8) << 3;
} else {
current_node_delta_serialization[0] |= 0b_0011_1000; // 7 << 3
latest_features.write(&mut current_node_delta_serialization).unwrap();
}
match strategy {
NodeSerializationStrategy::Mutated(MutatedNodeProperties { features: true, .. }) | NodeSerializationStrategy::Full => {
let latest_features = &node_delta.latest_details.as_ref().unwrap().features;
node_feature_update_count += 1;
node_has_update = true;

// are these features among the most common ones?
if let Some(index) = serialization_details.node_announcement_feature_defaults.iter().position(|f| f == latest_features) {
// this feature set is among the 6 defaults
current_node_delta_serialization[0] |= ((index + 1) as u8) << 3;
} else {
current_node_delta_serialization[0] |= 0b_0011_1000; // 7 << 3
latest_features.write(&mut current_node_delta_serialization).unwrap();
}
},
_ => {}
}

if node_has_update {
node_update_count += 1;
} else if matches!(strategy, NodeSerializationStrategy::Reminder) {
} else if let NodeSerializationStrategy::Reminder = strategy {
current_node_delta_serialization[0] |= 1 << 6;
}
}
Expand Down

0 comments on commit 73a56e3

Please sign in to comment.