1
Fork 0

Split StableCompare trait out of StableOrd trait.

StableCompare 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`.)
This commit is contained in:
Michael Woerister 2023-12-21 10:27:34 +01:00
parent 090d5eac72
commit 739e5ef49e
9 changed files with 118 additions and 22 deletions

View file

@ -4,7 +4,9 @@
use rustc_arena::DroplessArena;
use rustc_data_structures::fx::FxIndexSet;
use rustc_data_structures::stable_hasher::{HashStable, StableHasher, ToStableHashKey};
use rustc_data_structures::stable_hasher::{
HashStable, StableCompare, StableHasher, ToStableHashKey,
};
use rustc_data_structures::sync::Lock;
use rustc_macros::HashStable_Generic;
use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
@ -2103,6 +2105,14 @@ impl<CTX> ToStableHashKey<CTX> for Symbol {
}
}
impl StableCompare for Symbol {
const CAN_USE_UNSTABLE_SORT: bool = true;
fn stable_cmp(&self, other: &Self) -> std::cmp::Ordering {
self.as_str().cmp(other.as_str())
}
}
pub(crate) struct Interner(Lock<InternerInner>);
// The `&'static str`s in this type actually point into the arena.