Skip to content

Commit

Permalink
perf(identifier): use pattern matching
Browse files Browse the repository at this point in the history
way more efficient than manual slicing
  • Loading branch information
EdJoPaTo committed May 19, 2023
1 parent 3e92570 commit 837e24a
Showing 1 changed file with 5 additions and 8 deletions.
13 changes: 5 additions & 8 deletions src/identifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,9 @@ pub type TreeIdentifierVec = Vec<usize>;
/// assert_eq!(branch, []);
/// assert_eq!(leaf, None);
/// ```
pub fn get_without_leaf(identifier: TreeIdentifier) -> (TreeIdentifier, Option<&usize>) {
let length = identifier.len();
let length_without_leaf = length.saturating_sub(1);

let branch = &identifier[0..length_without_leaf];
let leaf = identifier.get(length_without_leaf);

(branch, leaf)
pub const fn get_without_leaf(identifier: TreeIdentifier) -> (TreeIdentifier, Option<&usize>) {
match identifier {
[branch @ .., leaf] => (branch, Some(leaf)),
[] => (&[] as &[usize], None),
}
}

0 comments on commit 837e24a

Please sign in to comment.