]> git.lizzy.rs Git - rust.git/commitdiff
Add `InternedString::with2`.
authorNicholas Nethercote <nnethercote@mozilla.com>
Thu, 9 May 2019 01:45:56 +0000 (11:45 +1000)
committerNicholas Nethercote <nnethercote@mozilla.com>
Fri, 10 May 2019 05:59:05 +0000 (15:59 +1000)
This lets comparisons occur with a single access to the interner,
instead of two.

src/libsyntax_pos/symbol.rs

index 20759217b54a0962f72fdf16511f6b3a8dfa35f0..107cf8360c4f9534be9f768736c8ae8a93f75163 100644 (file)
@@ -725,6 +725,15 @@ pub fn with<F: FnOnce(&str) -> R, R>(self, f: F) -> R {
         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 @@ fn partial_cmp(&self, other: &InternedString) -> Option<Ordering> {
         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 @@ fn cmp(&self, other: &InternedString) -> Ordering {
         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))
     }
 }