alef 0.67.3

Opinionated polyglot binding generator for Rust libraries
Documentation
//! Canonical ordering of the incoming [`ApiSurface`], applied at backend entry points.

use crate::core::ir::ApiSurface;

/// Returns a clone of `api` whose top-level item vectors are ordered by name.
///
/// Backends concatenate `types`/`enums`/`functions`/`errors` into a
/// single file per surface (`lib.rs`, a host-language facade, a stub file), in whatever order
/// those vectors happen to arrive in.
/// That incoming order reflects source-extraction and pipeline-orchestration order, which is not
/// guaranteed stable across separate `alef` invocations, so two runs over an unchanged tree could
/// emit the same blocks in a different order. Ordering once, here, at the backend entry points
/// makes the generated bytes a function of IR *content* only.
///
/// The sort is stable, so same-named entries (the cfg-gated duplicate pairs that
/// `ApiSurface::with_deduped_functions` collapses) keep their relative order. Apply this BEFORE
/// dedup: the dedup pass builds its `any(...)` cfg union and picks its canonical entry from
/// input index order, so it is itself order-sensitive. Per-item vectors (fields, variants,
/// methods, params) are deliberately left alone — their order is semantically meaningful. ~keep
pub(crate) fn with_sorted_items(api: &ApiSurface) -> ApiSurface {
    let mut sorted = api.clone();
    sorted.types.sort_by(|a, b| a.name.cmp(&b.name));
    sorted.enums.sort_by(|a, b| a.name.cmp(&b.name));
    sorted.functions.sort_by(|a, b| a.name.cmp(&b.name));
    sorted.errors.sort_by(|a, b| a.name.cmp(&b.name));
    sorted
}