Skip to content

Commit

Permalink
Use unwrap_or_else to avoid branch duplication [skip ci] (#229)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shatur authored Apr 14, 2024
1 parent 93c4a4c commit 13c3fbd
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/core/replication_rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,11 @@ pub struct ReplicationRules(Vec<ReplicationRule>);
impl ReplicationRules {
/// Inserts a new rule, maintaining sorting by their priority in descending order.
pub fn insert(&mut self, rule: ReplicationRule) {
match self.binary_search_by_key(&Reverse(rule.priority), |rule| Reverse(rule.priority)) {
Ok(index) => self.0.insert(index, rule),
Err(index) => self.0.insert(index, rule),
};
let index = self
.binary_search_by_key(&Reverse(rule.priority), |rule| Reverse(rule.priority))
.unwrap_or_else(|index| index);

self.0.insert(index, rule);
}
}

Expand Down Expand Up @@ -370,8 +371,8 @@ mod tests {
.replicate::<ComponentD>();

let replication_rules = app.world.resource::<ReplicationRules>();
let lens: Vec<_> = replication_rules.iter().map(|rule| rule.priority).collect();
assert_eq!(lens, [2, 2, 1, 1, 1, 1]);
let priorities: Vec<_> = replication_rules.iter().map(|rule| rule.priority).collect();
assert_eq!(priorities, [2, 2, 1, 1, 1, 1]);
}

#[derive(Serialize, Deserialize, Component)]
Expand Down

0 comments on commit 13c3fbd

Please sign in to comment.