about summary refs log tree commit diff
path: root/src/libserialize
diff options
context:
space:
mode:
authorAlex Crichton <alex@alexcrichton.com>2015-03-23 15:10:50 -0700
committerAlex Crichton <alex@alexcrichton.com>2015-03-23 15:10:50 -0700
commit753efb5042563dd34a4a524197fa14a129ddf449 (patch)
treec7f2301e6ba8d7e0e6f16674905a36c55b853085 /src/libserialize
parent388e5aee1e3df9e25f69815ffebfaa39e2167b5f (diff)
parent57cf2decf755c6eea3275e2a87862756eb8c62ca (diff)
downloadrust-753efb5042563dd34a4a524197fa14a129ddf449.tar.gz
rust-753efb5042563dd34a4a524197fa14a129ddf449.zip
rollup merge of #23601: nikomatsakis/by-value-index
This is a [breaking-change]. When indexing a generic map (hashmap, etc) using the `[]` operator, it is now necessary to borrow explicitly, so change `map[key]` to `map[&key]` (consistent with the `get` routine). However, indexing of string-valued maps with constant strings can now be written `map["abc"]`.

r? @japaric
cc @aturon @Gankro
Diffstat (limited to 'src/libserialize')
-rw-r--r--src/libserialize/json.rs23
1 files changed, 23 insertions, 0 deletions
diff --git a/src/libserialize/json.rs b/src/libserialize/json.rs
index 096c72e6af2..abbfc82319f 100644
--- a/src/libserialize/json.rs
+++ b/src/libserialize/json.rs
@@ -1218,6 +1218,7 @@ impl Json {
     }
 }
 
+#[cfg(stage0)]
 impl<'a> Index<&'a str>  for Json {
     type Output = Json;
 
@@ -1226,6 +1227,16 @@ impl<'a> Index<&'a str>  for Json {
     }
 }
 
+#[cfg(not(stage0))]
+impl<'a> Index<&'a str>  for Json {
+    type Output = Json;
+
+    fn index(&self, idx: &'a str) -> &Json {
+        self.find(idx).unwrap()
+    }
+}
+
+#[cfg(stage0)]
 impl Index<uint> for Json {
     type Output = Json;
 
@@ -1237,6 +1248,18 @@ impl Index<uint> for Json {
     }
 }
 
+#[cfg(not(stage0))]
+impl Index<uint> for Json {
+    type Output = Json;
+
+    fn index<'a>(&'a self, idx: uint) -> &'a Json {
+        match self {
+            &Json::Array(ref v) => &v[idx],
+            _ => panic!("can only index Json with uint if it is an array")
+        }
+    }
+}
+
 /// The output of the streaming parser.
 #[derive(PartialEq, Clone, Debug)]
 pub enum JsonEvent {