diff options
| author | Orson Peters <orsonpeters@gmail.com> | 2025-07-26 20:13:07 +0200 |
|---|---|---|
| committer | Orson Peters <orsonpeters@gmail.com> | 2025-07-29 22:15:44 +0200 |
| commit | 05da623016f7bfed812261f2793b4b589631a6b0 (patch) | |
| tree | f4042d9b1334d2f2c461d5458f5ebbb4d95a4ce4 /library/core | |
| parent | 133798f9fb2887bcccf1098937540e280dd58465 (diff) | |
| download | rust-05da623016f7bfed812261f2793b4b589631a6b0.tar.gz rust-05da623016f7bfed812261f2793b4b589631a6b0.zip | |
Fix Ord, Eq and Hash implementation of panic::Location
Faster equality compare Add tests Add missing files for tests
Diffstat (limited to 'library/core')
| -rw-r--r-- | library/core/src/panic/location.rs | 42 |
1 files changed, 41 insertions, 1 deletions
diff --git a/library/core/src/panic/location.rs b/library/core/src/panic/location.rs index 97227020885..6ef7d5a22a3 100644 --- a/library/core/src/panic/location.rs +++ b/library/core/src/panic/location.rs @@ -1,5 +1,7 @@ +use crate::cmp::Ordering; use crate::ffi::CStr; use crate::fmt; +use crate::hash::{Hash, Hasher}; use crate::marker::PhantomData; use crate::ptr::NonNull; @@ -32,7 +34,7 @@ use crate::ptr::NonNull; /// Files are compared as strings, not `Path`, which could be unexpected. /// See [`Location::file`]'s documentation for more discussion. #[lang = "panic_location"] -#[derive(Copy, Clone, Eq, Hash, Ord, PartialEq, PartialOrd)] +#[derive(Copy, Clone)] #[stable(feature = "panic_hooks", since = "1.10.0")] pub struct Location<'a> { // A raw pointer is used rather than a reference because the pointer is valid for one more byte @@ -45,6 +47,44 @@ pub struct Location<'a> { } #[stable(feature = "panic_hooks", since = "1.10.0")] +impl PartialEq for Location<'_> { + fn eq(&self, other: &Self) -> bool { + // Compare col / line first as they're cheaper to compare and more likely to differ, + // while not impacting the result. + self.col == other.col && self.line == other.line && self.file() == other.file() + } +} + +#[stable(feature = "panic_hooks", since = "1.10.0")] +impl Eq for Location<'_> {} + +#[stable(feature = "panic_hooks", since = "1.10.0")] +impl Ord for Location<'_> { + fn cmp(&self, other: &Self) -> Ordering { + self.file() + .cmp(other.file()) + .then_with(|| self.line.cmp(&other.line)) + .then_with(|| self.col.cmp(&other.col)) + } +} + +#[stable(feature = "panic_hooks", since = "1.10.0")] +impl PartialOrd for Location<'_> { + fn partial_cmp(&self, other: &Self) -> Option<Ordering> { + Some(self.cmp(other)) + } +} + +#[stable(feature = "panic_hooks", since = "1.10.0")] +impl Hash for Location<'_> { + fn hash<H: Hasher>(&self, state: &mut H) { + self.file().hash(state); + self.line.hash(state); + self.col.hash(state); + } +} + +#[stable(feature = "panic_hooks", since = "1.10.0")] impl fmt::Debug for Location<'_> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_struct("Location") |
