Skip to content

Commit 8c465b4

Browse files
committed
Add InternedString::with2.
This lets comparisons occur with a single access to the interner, instead of two.
1 parent 33cde4a commit 8c465b4

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

src/libsyntax_pos/symbol.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -725,6 +725,15 @@ impl InternedString {
725725
unsafe { f(&*str) }
726726
}
727727

728+
fn with2<F: FnOnce(&str, &str) -> R, R>(self, other: &InternedString, f: F) -> R {
729+
let (self_str, other_str) = with_interner(|interner| {
730+
(interner.get(self.symbol) as *const str,
731+
interner.get(other.symbol) as *const str)
732+
});
733+
// This is safe for the same reason that `with` is safe.
734+
unsafe { f(&*self_str, &*other_str) }
735+
}
736+
728737
pub fn as_symbol(self) -> Symbol {
729738
self.symbol
730739
}
@@ -745,7 +754,7 @@ impl PartialOrd<InternedString> for InternedString {
745754
if self.symbol == other.symbol {
746755
return Some(Ordering::Equal);
747756
}
748-
self.with(|self_str| other.with(|other_str| self_str.partial_cmp(other_str)))
757+
self.with2(other, |self_str, other_str| self_str.partial_cmp(other_str))
749758
}
750759
}
751760

@@ -754,7 +763,7 @@ impl Ord for InternedString {
754763
if self.symbol == other.symbol {
755764
return Ordering::Equal;
756765
}
757-
self.with(|self_str| other.with(|other_str| self_str.cmp(&other_str)))
766+
self.with2(other, |self_str, other_str| self_str.cmp(other_str))
758767
}
759768
}
760769

0 commit comments

Comments
 (0)