1
Fork 0

Adjust Index/IndexMut impls. For generic collections, we take

references. For collections whose keys are integers, we take both
references and by-value.
This commit is contained in:
Niko Matsakis 2015-03-21 19:33:27 -04:00
parent bc1dde468c
commit b4d4daf007
12 changed files with 600 additions and 8 deletions

View file

@ -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 {