Repository: diesel-rs/diesel
File: diesel/src/associations/belongs_to.rs (Rust)

Code before:
    Child: BelongsTo<Parent>,
    &'a Parent: Identifiable,
    Id<&'a Parent>: Borrow<Child::ForeignKey>,
{
    fn grouped_by(self, parents: &'a [Parent]) -> Vec<Vec<Child>> {
        use std::collections::HashMap;

        let id_indices: HashMap<_, _> = parents
            .iter()
            .enumerate()
            .map(|(i, u)| (u.id(), i))
            .collect();
        let mut result = parents.iter().map(|_| Vec::new()).collect::<Vec<_>>();
        for child in self {
            if let Some(index) = child.foreign_key().map(|i| id_indices[i]) {
                result[index].push(child);
            }
        }
        result
    }

    fn try_grouped_by(
        self,
        parents: &'a [Parent],
    ) -> Result<Vec<Vec<Child>>, TryGroupedByError<Child>> {
        use std::{collections::HashMap, iter};

        let mut grouped: Vec<_> = iter::repeat_with(Vec::new).take(parents.len()).collect();
        let mut ungrouped: Vec<_> = Vec::new();

        let id_indices: HashMap<_, _> = parents
            .iter()
            .enumerate()
            .map(|(i, u)| (u.id(), i))
            .collect();

        for child in self {
            child
                .foreign_key()
                .and_then(|i| id_indices.get(i))
                .map_or(&mut ungrouped, |i| &mut grouped[*i])
                .push(child);
        }

        if ungrouped.is_empty() {
            Ok(grouped)
        } else {
            Err(TryGroupedByError { grouped, ungrouped })
        }
    }
}

Review comment (suggestion):
```suggestion
        use std::collections::HashMap;
        use std::iter;
```

to keep this consistent with the other imports in diesel.

Code after:
    ///
    /// This is generally used by methods like [`GroupedBy::try_grouped_by`].
    pub fn new(grouped: Vec<Vec<Child>>, ungrouped: Vec<Child>) -> Self {
        Self { grouped, ungrouped }
    }
}

impl<'a, Parent: 'a, Child, Iter> GroupedBy<'a, Parent> for Iter
where
    Iter: IntoIterator<Item = Child>,
    Child: BelongsTo<Parent>,
    &'a Parent: Identifiable,
    Id<&'a Parent>: Borrow<Child::ForeignKey>,
{
    fn grouped_by(self, parents: &'a [Parent]) -> Vec<Vec<Child>> {
        use std::collections::HashMap;

        let id_indices: HashMap<_, _> = parents
            .iter()
            .enumerate()
            .map(|(i, u)| (u.id(), i))
            .collect();
        let mut result = parents.iter().map(|_| Vec::new()).collect::<Vec<_>>();
        for child in self {
            if let Some(index) = child.foreign_key().map(|i| id_indices[i]) {
                result[index].push(child);
            }
        }
        result
    }

    fn try_grouped_by(
        self,
        parents: &'a [Parent],
    ) -> Result<Vec<Vec<Child>>, TryGroupedByError<Child>> {
        use std::collections::HashMap;
        use std::iter;

        let mut grouped: Vec<_> = iter::repeat_with(Vec::new).take(parents.len()).collect();
        let mut ungrouped: Vec<_> = Vec::new();

        let id_indices: HashMap<_, _> = parents
            .iter()
            .enumerate()
            .map(|(i, u)| (u.id(), i))
            .collect();

        for child in self {
            child
                .foreign_key()
                .and_then(|i| id_indices.get(i))