Skip to content

Commit c94e3a7

Browse files
committed
refactor: remove unnecessary operation
1 parent a5aad0e commit c94e3a7

1 file changed

Lines changed: 8 additions & 2 deletions

File tree

src/data_tree/reflection/par_methods.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ where
2121
.find(|(_, child)| child.size > size);
2222
if let Some((index, _)) = excess_child {
2323
let path = once(name).collect();
24-
let mut children = children;
25-
let child = children.swap_remove(index); // this still does the unnecessary work of swapping the elements, how to skip it?
24+
let child = keep_one(children, index).expect("excess child");
2625
return Err(ConversionError::ExcessiveChildren { path, size, child });
2726
}
2827
let children: Result<Vec<_>, _> = children
@@ -89,3 +88,10 @@ where
8988
})
9089
}
9190
}
91+
92+
/// Extract an item at `index` if it exists. Then drop all remaining items.
93+
#[inline]
94+
fn keep_one<Item>(vec: Vec<Item>, index: usize) -> Option<Item> {
95+
// worry not about performance, for `vec.advanced_by` is overridden with O(1) algorithm
96+
vec.into_iter().nth(index)
97+
}

0 commit comments

Comments
 (0)