icu4x icon indicating copy to clipboard operation
icu4x copied to clipboard

Add `total_cmp` helper functions to icu_locid types and DataLocale

Open sffc opened this issue 1 year ago • 1 comments

We do not implement Ord on Locale or LanguageIdentifier because there are multiple possible orderings, and we did not want to privilege one ordering over another (see https://github.com/unicode-org/icu4x/issues/1215).

We export a strict_cmp function that compares the receiver with a byte array. However, we do not export a function that compares the receiver to another object of the same type. This had led to issues such as https://github.com/unicode-org/icu4x/pull/3588. I also stumbled across this today when trying to make a BTreeSet of locales.

In order to serve this use case, I added the following function to the types that needed it:

    /// Returns an ordering suitable for use in [`BTreeSet`].
    ///
    /// The ordering may or may not be equivalent to string ordering, and it
    /// may or may not be stable across ICU4X releases.
    ///
    /// [`BTreeSet`]: alloc::collections::BTreeSet
    pub fn total_cmp(&self, other: &Self) -> Ordering

This gives us flexibility moving forward to tweak the impl or add additional comparison functions producing different orderings.

sffc avatar Feb 14 '24 02:02 sffc

not 100% convinced this is needed over FFI but it's a weak opinion

Manishearth avatar Feb 14 '24 05:02 Manishearth