Streamline code
All checks were successful
Cargo CI / Build and Test (pull_request) Successful in 1m41s
Cargo CI / Lint (pull_request) Successful in 1m13s

This commit is contained in:
Wojciech Kozlowski 2024-03-02 18:43:58 +01:00
parent c77b4acbd4
commit 0fee810040

View File

@ -86,17 +86,19 @@ pub trait WithId {
fn id(&self) -> &Self::Id; fn id(&self) -> &Self::Id;
} }
pub struct MergeCollections<ID, T> { pub struct MergeCollections<ID, T, IT> {
_id: PhantomData<ID>, _id: PhantomData<ID>,
_t: PhantomData<T>, _t: PhantomData<T>,
_it: PhantomData<IT>,
} }
impl<ID, T> MergeCollections<ID, T> impl<ID, T, IT> MergeCollections<ID, T, IT>
where where
ID: Eq + Hash + Clone, ID: Eq + Hash + Clone,
T: WithId<Id = ID> + Merge + Ord, T: WithId<Id = ID> + Merge + Ord,
IT: IntoIterator<Item = T>,
{ {
pub fn merge_iter<IT: IntoIterator<Item = T>>(primary: IT, secondary: IT) -> Vec<T> { pub fn merge_iter(primary: IT, secondary: IT) -> Vec<T> {
let primary = primary let primary = primary
.into_iter() .into_iter()
.map(|item| (item.id().clone(), item)) .map(|item| (item.id().clone(), item))
@ -104,7 +106,7 @@ where
Self::merge(primary, secondary) Self::merge(primary, secondary)
} }
pub fn merge<IT: IntoIterator<Item = T>>(mut primary: HashMap<ID, T>, secondary: IT) -> Vec<T> { pub fn merge(mut primary: HashMap<ID, T>, secondary: IT) -> Vec<T> {
for secondary_item in secondary { for secondary_item in secondary {
if let Some(ref mut primary_item) = primary.get_mut(secondary_item.id()) { if let Some(ref mut primary_item) = primary.get_mut(secondary_item.id()) {
primary_item.merge_in_place(secondary_item); primary_item.merge_in_place(secondary_item);