Skip to main content

ordered_components

Function ordered_components 

Source
pub fn ordered_components(grouped: &GroupedQuantity) -> Vec<&Quantity>
Expand description

The components of grouped, ordered by unit name, the unitless component first.

§The rule

Components are ordered by unit name, with the unitless component first — it counts as an empty unit name — and components sharing a unit keep the order they were added.

§Why this exists

A GroupedQuantity holds the quantities that could not be added into one another: 1 cup of flour plus 100 g of flour stays two components, because no conversion between them exists. It keeps the ones whose unit the converter does not know in a HashMap keyed by unit name, and Rust randomises HashMap iteration order per process, so GroupedQuantity::iter — and the Display impl built on it — yield those components in a different order on every run:

flour 1 cup, 100 g
flour 100 g, 1 cup

This became visible when cooklang’s bundled_units feature was switched off so quantities keep the units they were authored in: without the unit database no unit is “known”, so every component lands in that map and any ingredient measured two ways prints in random order.

The order the units were written in cannot be recovered here — the map has already lost it — so ordering by unit name is chosen instead: it is implementable from the data at hand, identical on every run and platform (byte-wise comparison of the unit text, no locale involved), and short enough to explain in one sentence.