StableCompare in rustc_data_structures::stable_hasher - Rust
Description: This is a companion trait to `StableOrd`. Some types like `Symbol` can be compared in a cross-session stable way, but their `Ord` implementation is not stable. In such cases, a `StableOrd` implementation can be provided to offer a lightweight way for stable sorting. (The more heavyweight option is to sort via `ToStableHashKey`, but then sorting needs to have access to a stable hashing context and `ToStableHashKey` can also be expensive as in the case of `Symbol` where it has to allocate a `String`.)

Skip to main content

## StableCompare

# Trait StableCompare

Source

    pub trait StableCompare {
        const CAN_USE_UNSTABLE_SORT: bool;

        // Required method
        fn stable_cmp(&self, other: &Self) -> Ordering;
    }

Expand description

This is a companion trait to `StableOrd`. Some types like `Symbol` can be compared in a cross-session stable way, but their `Ord` implementation is not stable. In such cases, a `StableOrd` implementation can be provided to offer a lightweight way for stable sorting. (The more heavyweight option is to sort via `ToStableHashKey`, but then sorting needs to have access to a stable hashing context and `ToStableHashKey` can also be expensive as in the case of `Symbol` where it has to allocate a `String`.)

See the documentation of StableOrd for how stable sort order is defined. The same definition applies here. Be careful when implementing this trait.

## Required Associated Constants§

Source

#### const CAN_USE_UNSTABLE_SORT: bool

## Required Methods§

Source

#### fn stable_cmp(&self, other: &Self) -> Ordering

## Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

## Implementors§

Source§

### impl<T: StableOrd> StableCompare for T

`StableOrd` denotes that the type’s `Ord` implementation is stable, so we can implement `StableCompare` by just delegating to `Ord`.

Source§

#### const CAN_USE_UNSTABLE_SORT: bool = T::CAN_USE_UNSTABLE_SORT