Skip to content

Commit

Permalink
Add InternedString::with2.
Browse files Browse the repository at this point in the history
This lets comparisons occur with a single access to the interner,
instead of two.
  • Loading branch information
nnethercote committed May 10, 2019
1 parent 33cde4a commit 8c465b4
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/libsyntax_pos/symbol.rs
Expand Up @@ -725,6 +725,15 @@ impl InternedString {
unsafe { f(&*str) }
}

fn with2<F: FnOnce(&str, &str) -> R, R>(self, other: &InternedString, f: F) -> R {
let (self_str, other_str) = with_interner(|interner| {
(interner.get(self.symbol) as *const str,
interner.get(other.symbol) as *const str)
});
// This is safe for the same reason that `with` is safe.
unsafe { f(&*self_str, &*other_str) }
}

pub fn as_symbol(self) -> Symbol {
self.symbol
}
Expand All @@ -745,7 +754,7 @@ impl PartialOrd<InternedString> for InternedString {
if self.symbol == other.symbol {
return Some(Ordering::Equal);
}
self.with(|self_str| other.with(|other_str| self_str.partial_cmp(other_str)))
self.with2(other, |self_str, other_str| self_str.partial_cmp(other_str))
}
}

Expand All @@ -754,7 +763,7 @@ impl Ord for InternedString {
if self.symbol == other.symbol {
return Ordering::Equal;
}
self.with(|self_str| other.with(|other_str| self_str.cmp(&other_str)))
self.with2(other, |self_str, other_str| self_str.cmp(other_str))
}
}

Expand Down

0 comments on commit 8c465b4

Please sign in to comment.