diff options
| author | Nicholas Nethercote <nnethercote@mozilla.com> | 2019-05-09 11:45:56 +1000 |
|---|---|---|
| committer | Nicholas Nethercote <nnethercote@mozilla.com> | 2019-05-10 15:59:05 +1000 |
| commit | 8c465b4efab9dec55434193acacf0a10e83c3ae0 (patch) | |
| tree | e06fd02117df83fe59874794c527c31fcc9ad3e7 /src | |
| parent | 33cde4aac2ec1b0a493d0acaa4c4fb45de0c6e94 (diff) | |
| download | rust-8c465b4efab9dec55434193acacf0a10e83c3ae0.tar.gz rust-8c465b4efab9dec55434193acacf0a10e83c3ae0.zip | |
Add `InternedString::with2`.
This lets comparisons occur with a single access to the interner, instead of two.
Diffstat (limited to 'src')
| -rw-r--r-- | src/libsyntax_pos/symbol.rs | 13 |
1 files changed, 11 insertions, 2 deletions
diff --git a/src/libsyntax_pos/symbol.rs b/src/libsyntax_pos/symbol.rs index 20759217b54..107cf8360c4 100644 --- a/src/libsyntax_pos/symbol.rs +++ b/src/libsyntax_pos/symbol.rs @@ -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 } @@ -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)) } } @@ -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)) } } |
