Repository: typst/typst
File: crates/typst-layout/src/math/run.rs (Rust)

Code before:
            if fragment.class() == MathClass::Vary
                && matches!(
                    last.map(|i| resolved[i].class()),
                    Some(
                        MathClass::Normal
                            | MathClass::Alphabetic
                            | MathClass::Closing
                            | MathClass::Fence
                    )
                )
            {
                fragment.set_class(MathClass::Binary);
            }

            // Insert spacing between the last and this non-ignorant item.
            if !fragment.is_ignorant() {
                if let Some(i) = last
                    && let Some(s) = spacing(&resolved[i], space.take(), &fragment)
                {
                    let idx = if matches!(resolved.last(), Some(MathFragment::Align)) {
                        // Only one alignment point should be skipped when
                        // inserting spacing.
                        resolved.last_index().unwrap()
                    } else {
                        i + 1
                    };
                    resolved.insert(idx, s);
                }

                last = Some(resolved.len());
            }

            resolved.push(fragment);
        }

        if let Some(idx) = resolved.last_index()
            && let MathFragment::Spacing(_, true) = resolved.0[idx]
        {
            resolved.0.remove(idx);
        }

        Self(resolved.0)
    }

    pub fn iter(&self) -> std::slice::Iter<'_, MathFragment> {
        self.0.iter()
    }

    /// Split by linebreaks, and copy [`MathFragment`]s into rows.
    pub fn rows(&self) -> Vec<Self> {
        self.0

Review comment (suggestion):
```suggestion
                    // Only one alignment point should be skipped when inserting
                    // spacing.
                    let idx = resolved
                        .last_index()
                        .filter(|&k| matches!(resolved[k], MathFragment::Align))
                        .unwrap_or(i + 1);
```
That way we could skip the unwrap and only search once. The `last()` method can be removed then.

Code after:
            if fragment.class() == MathClass::Vary
                && matches!(
                    last.map(|i| resolved[i].class()),
                    Some(
                        MathClass::Normal
                            | MathClass::Alphabetic
                            | MathClass::Closing
                            | MathClass::Fence
                    )
                )
            {
                fragment.set_class(MathClass::Binary);
            }

            // Insert spacing between the last and this non-ignorant item.
            if !fragment.is_ignorant() {
                if let Some(i) = last
                    && let Some(s) = spacing(&resolved[i], space.take(), &fragment)
                {
                    // Only one alignment point should be skipped when inserting
                    // spacing.
                    let idx = resolved
                        .last_index()
                        .filter(|&k| matches!(resolved[k], MathFragment::Align))
                        .unwrap_or(i + 1);

                    resolved.insert(idx, s);
                }

                last = Some(resolved.len());
            }

            resolved.push(fragment);
        }

        if let Some(idx) = resolved.last_index()
            && let MathFragment::Spacing(_, true) = resolved.0[idx]
        {
            resolved.0.remove(idx);
        }

        Self(resolved.0)
    }

    pub fn iter(&self) -> std::slice::Iter<'_, MathFragment> {
        self.0.iter()
    }

    /// Split by linebreaks, and copy [`MathFragment`]s into rows.
    pub fn rows(&self) -> Vec<Self> {
        self.0