about summary refs log tree commit diff
path: root/src/libsyntax/util
diff options
context:
space:
mode:
authorMarcin Fatyga <marcinf@google.com>2016-11-01 15:26:22 +0100
committerMarcin Fatyga <marcinf@google.com>2016-11-01 15:26:22 +0100
commit655effedf25e2039d283b839429bf2f42b7012a4 (patch)
tree34fd087d891556c70a14b26a90d1bdccd0a7ccb2 /src/libsyntax/util
parent4e2822c5c28bb342e5862ba7cc0b90b865c68be1 (diff)
parentac968c466451cb9aafd9e8598ddb396ed0e6fe31 (diff)
downloadrust-655effedf25e2039d283b839429bf2f42b7012a4.tar.gz
rust-655effedf25e2039d283b839429bf2f42b7012a4.zip
Merge branch 'master' of https://github.com/rust-lang/rust
Conflicts:
	src/libcoretest/lib.rs
Diffstat (limited to 'src/libsyntax/util')
-rw-r--r--src/libsyntax/util/interner.rs40
-rw-r--r--src/libsyntax/util/parser_testing.rs5
-rw-r--r--src/libsyntax/util/small_vector.rs56
3 files changed, 59 insertions, 42 deletions
diff --git a/src/libsyntax/util/interner.rs b/src/libsyntax/util/interner.rs
index 6bb409715aa..f56c6cedcd1 100644
--- a/src/libsyntax/util/interner.rs
+++ b/src/libsyntax/util/interner.rs
@@ -14,23 +14,13 @@
 
 use ast::Name;
 
-use std::borrow::Borrow;
 use std::collections::HashMap;
 use std::rc::Rc;
 
-#[derive(PartialEq, Eq, Hash)]
-struct RcStr(Rc<String>);
-
-impl Borrow<str> for RcStr {
-    fn borrow(&self) -> &str {
-        &self.0
-    }
-}
-
 #[derive(Default)]
 pub struct Interner {
-    names: HashMap<RcStr, Name>,
-    strings: Vec<Rc<String>>,
+    names: HashMap<Rc<str>, Name>,
+    strings: Vec<Rc<str>>,
 }
 
 /// When traits can extend traits, we should extend index<Name,T> to get []
@@ -47,22 +37,22 @@ impl Interner {
         this
     }
 
-    pub fn intern<T: Borrow<str> + Into<String>>(&mut self, string: T) -> Name {
-        if let Some(&name) = self.names.get(string.borrow()) {
+    pub fn intern(&mut self, string: &str) -> Name {
+        if let Some(&name) = self.names.get(string) {
             return name;
         }
 
         let name = Name(self.strings.len() as u32);
-        let string = Rc::new(string.into());
+        let string = Rc::__from_str(string);
         self.strings.push(string.clone());
-        self.names.insert(RcStr(string), name);
+        self.names.insert(string, name);
         name
     }
 
     pub fn gensym(&mut self, string: &str) -> Name {
         let gensym = Name(self.strings.len() as u32);
         // leave out of `names` to avoid colliding
-        self.strings.push(Rc::new(string.to_owned()));
+        self.strings.push(Rc::__from_str(string));
         gensym
     }
 
@@ -75,7 +65,7 @@ impl Interner {
         gensym
     }
 
-    pub fn get(&self, name: Name) -> Rc<String> {
+    pub fn get(&self, name: Name) -> Rc<str> {
         self.strings[name.0 as usize].clone()
     }
 
@@ -109,13 +99,13 @@ mod tests {
         assert_eq!(i.gensym("dog"), Name(4));
         // gensym tests again with gensym_copy:
         assert_eq!(i.gensym_copy(Name(2)), Name(5));
-        assert_eq!(*i.get(Name(5)), "zebra");
+        assert_eq!(&*i.get(Name(5)), "zebra");
         assert_eq!(i.gensym_copy(Name(2)), Name(6));
-        assert_eq!(*i.get(Name(6)), "zebra");
-        assert_eq!(*i.get(Name(0)), "dog");
-        assert_eq!(*i.get(Name(1)), "cat");
-        assert_eq!(*i.get(Name(2)), "zebra");
-        assert_eq!(*i.get(Name(3)), "zebra");
-        assert_eq!(*i.get(Name(4)), "dog");
+        assert_eq!(&*i.get(Name(6)), "zebra");
+        assert_eq!(&*i.get(Name(0)), "dog");
+        assert_eq!(&*i.get(Name(1)), "cat");
+        assert_eq!(&*i.get(Name(2)), "zebra");
+        assert_eq!(&*i.get(Name(3)), "zebra");
+        assert_eq!(&*i.get(Name(4)), "dog");
     }
 }
diff --git a/src/libsyntax/util/parser_testing.rs b/src/libsyntax/util/parser_testing.rs
index f59428bf536..76d3f2a063c 100644
--- a/src/libsyntax/util/parser_testing.rs
+++ b/src/libsyntax/util/parser_testing.rs
@@ -25,10 +25,7 @@ pub fn string_to_tts(source_str: String) -> Vec<tokenstream::TokenTree> {
 
 /// Map string to parser (via tts)
 pub fn string_to_parser<'a>(ps: &'a ParseSess, source_str: String) -> Parser<'a> {
-    new_parser_from_source_str(ps,
-                               Vec::new(),
-                               "bogofile".to_string(),
-                               source_str)
+    new_parser_from_source_str(ps, "bogofile".to_string(), source_str)
 }
 
 fn with_error_checking_parse<'a, T, F>(s: String, ps: &'a ParseSess, f: F) -> T where
diff --git a/src/libsyntax/util/small_vector.rs b/src/libsyntax/util/small_vector.rs
index 373dfc4ddfa..9be7dbd6817 100644
--- a/src/libsyntax/util/small_vector.rs
+++ b/src/libsyntax/util/small_vector.rs
@@ -11,6 +11,7 @@
 use self::SmallVectorRepr::*;
 use self::IntoIterRepr::*;
 
+use core::ops;
 use std::iter::{IntoIterator, FromIterator};
 use std::mem;
 use std::slice;
@@ -19,10 +20,12 @@ use std::vec;
 use util::move_map::MoveMap;
 
 /// A vector type optimized for cases where the size is almost always 0 or 1
+#[derive(Clone)]
 pub struct SmallVector<T> {
     repr: SmallVectorRepr<T>,
 }
 
+#[derive(Clone)]
 enum SmallVectorRepr<T> {
     Zero,
     One(T),
@@ -75,16 +78,11 @@ impl<T> SmallVector<T> {
     }
 
     pub fn as_slice(&self) -> &[T] {
-        match self.repr {
-            Zero => {
-                let result: &[T] = &[];
-                result
-            }
-            One(ref v) => {
-                unsafe { slice::from_raw_parts(v, 1) }
-            }
-            Many(ref vs) => vs
-        }
+        self
+    }
+
+    pub fn as_mut_slice(&mut self) -> &mut [T] {
+        self
     }
 
     pub fn pop(&mut self) -> Option<T> {
@@ -107,7 +105,7 @@ impl<T> SmallVector<T> {
             One(..) => {
                 let one = mem::replace(&mut self.repr, Zero);
                 match one {
-                    One(v1) => mem::replace(&mut self.repr, Many(vec!(v1, v))),
+                    One(v1) => mem::replace(&mut self.repr, Many(vec![v1, v])),
                     _ => unreachable!()
                 };
             }
@@ -163,6 +161,38 @@ impl<T> SmallVector<T> {
     }
 }
 
+impl<T> ops::Deref for SmallVector<T> {
+    type Target = [T];
+
+    fn deref(&self) -> &[T] {
+        match self.repr {
+            Zero => {
+                let result: &[T] = &[];
+                result
+            }
+            One(ref v) => {
+                unsafe { slice::from_raw_parts(v, 1) }
+            }
+            Many(ref vs) => vs
+        }
+    }
+}
+
+impl<T> ops::DerefMut for SmallVector<T> {
+    fn deref_mut(&mut self) -> &mut [T] {
+        match self.repr {
+            Zero => {
+                let result: &mut [T] = &mut [];
+                result
+            }
+            One(ref mut v) => {
+                unsafe { slice::from_raw_parts_mut(v, 1) }
+            }
+            Many(ref mut vs) => vs
+        }
+    }
+}
+
 impl<T> IntoIterator for SmallVector<T> {
     type Item = T;
     type IntoIter = IntoIter<T>;
@@ -284,12 +314,12 @@ mod tests {
     #[test]
     #[should_panic]
     fn test_expect_one_many() {
-        SmallVector::many(vec!(1, 2)).expect_one("");
+        SmallVector::many(vec![1, 2]).expect_one("");
     }
 
     #[test]
     fn test_expect_one_one() {
         assert_eq!(1, SmallVector::one(1).expect_one(""));
-        assert_eq!(1, SmallVector::many(vec!(1)).expect_one(""));
+        assert_eq!(1, SmallVector::many(vec![1]).expect_one(""));
     }
 }