From b4a4ed72d7b13fb8012878a191c7ca15de995030 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Tue, 15 Jul 2014 17:30:00 -0700 Subject: Migrate to a TreeMap for determinism --- src/parser.rs | 16 ++++++++-------- src/serialization.rs | 26 +++++++++++++------------- src/show.rs | 4 ++-- src/toml.rs | 6 +++--- 4 files changed, 26 insertions(+), 26 deletions(-) diff --git a/src/parser.rs b/src/parser.rs index f97645d..ea1b3eb 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -1,5 +1,5 @@ use std::char; -use std::collections::{HashMap, HashSet}; +use std::collections::{TreeMap, HashSet}; use std::num::FromStrRadix; use std::str; @@ -136,7 +136,7 @@ impl<'a> Parser<'a> { /// If an error occurs, the `errors` field of this parser can be consulted /// to determine the cause of the parse failure. pub fn parse(&mut self) -> Option { - let mut ret = HashMap::new(); + let mut ret = TreeMap::new(); loop { self.ws(); match self.cur.clone().next() { @@ -172,7 +172,7 @@ impl<'a> Parser<'a> { return None } - let mut table = HashMap::new(); + let mut table = TreeMap::new(); if !self.values(&mut table) { return None } if array { self.insert_array(&mut ret, section, Table(table), start) @@ -559,7 +559,7 @@ impl<'a> Parser<'a> { let tmp = cur; if tmp.contains_key(&part) { - match *tmp.get_mut(&part) { + match *tmp.find_mut(&part).unwrap() { Table(ref mut table) => { cur = table; continue @@ -592,8 +592,8 @@ impl<'a> Parser<'a> { } // Initialize an empty table as part of this sub-key - tmp.insert(part.clone(), Table(HashMap::new())); - match *tmp.get_mut(&part) { + tmp.insert(part.clone(), Table(TreeMap::new())); + match *tmp.find_mut(&part).unwrap() { Table(ref mut inner) => cur = inner, _ => unreachable!(), } @@ -618,7 +618,7 @@ impl<'a> Parser<'a> { }; let key = key.to_string(); if !into.contains_key(&key) { - into.insert(key.clone(), Table(HashMap::new())); + into.insert(key.clone(), Table(TreeMap::new())); } match into.find_mut(&key) { Some(&Table(ref mut table)) => { @@ -653,7 +653,7 @@ impl<'a> Parser<'a> { if !into.contains_key(&key) { into.insert(key.clone(), Array(Vec::new())); } - match *into.get_mut(&key) { + match *into.find_mut(&key).unwrap() { Array(ref mut vec) => { match vec.as_slice().head() { Some(ref v) if !v.same_type(&value) => { diff --git a/src/serialization.rs b/src/serialization.rs index deec85c..4397294 100644 --- a/src/serialization.rs +++ b/src/serialization.rs @@ -1,4 +1,4 @@ -use std::collections::HashMap; +use std::collections::TreeMap; use std::mem; use std::fmt; @@ -124,7 +124,7 @@ pub fn encode_str>(t: &T) -> String { impl Encoder { /// Constructs a new encoder which will emit to the given output stream. pub fn new() -> Encoder { - Encoder { state: Start, toml: HashMap::new() } + Encoder { state: Start, toml: TreeMap::new() } } fn emit_value(&mut self, v: Value) -> Result<(), Error> { @@ -667,8 +667,8 @@ impl serialize::Decoder for Decoder { { match self.toml { Some(Table(ref table)) => { - match table.keys().skip(idx).next() { - Some(key) => { + match table.iter().skip(idx).next() { + Some((key, _)) => { f(&mut self.sub_decoder(Some(String(key.to_string())), key.as_slice())) } @@ -684,10 +684,10 @@ impl serialize::Decoder for Decoder { { match self.toml { Some(Table(ref table)) => { - match table.values().skip(idx).next() { - Some(key) => { + match table.iter().skip(idx).next() { + Some((_, value)) => { // XXX: this shouldn't clone - f(&mut self.sub_decoder(Some(key.clone()), "")) + f(&mut self.sub_decoder(Some(value.clone()), "")) } None => Err(self.err(ExpectedMapElement(idx))), } @@ -743,7 +743,7 @@ impl fmt::Show for DecodeError { #[cfg(test)] mod tests { - use std::collections::{HashMap, HashSet}; + use std::collections::{TreeMap, HashSet}; use serialize::{Encodable, Decodable}; use super::{Encoder, Decoder, DecodeError}; @@ -761,7 +761,7 @@ mod tests { }) ) macro_rules! map( ($($k:ident: $v:expr),*) => ({ - let mut _m = HashMap::new(); + let mut _m = TreeMap::new(); $(_m.insert(stringify!($k).to_string(), $v);)* _m }) ) @@ -785,7 +785,7 @@ mod tests { assert_eq!(encode!(v), map! { a_b: Integer(2) }); assert_eq!(v, decode!(Table(encode!(v)))); - let mut m = HashMap::new(); + let mut m = TreeMap::new(); m.insert("a-b".to_string(), Integer(2)); assert_eq!(v, decode!(Table(encode!(v)))); } @@ -884,13 +884,13 @@ mod tests { fn hashmap() { #[deriving(Encodable, Decodable, PartialEq, Show)] struct Foo { - map: HashMap, + map: TreeMap, set: HashSet, } let v = Foo { map: { - let mut m = HashMap::new(); + let mut m = TreeMap::new(); m.insert("foo".to_string(), 10); m.insert("bar".to_string(), 4); m @@ -1084,7 +1084,7 @@ mod tests { #[test] fn unused_fields4() { #[deriving(Encodable, Decodable, PartialEq, Show)] - struct Foo { a: HashMap } + struct Foo { a: TreeMap } let v = Foo { a: map! { a: "foo".to_string() } }; let mut d = Decoder::new(Table(map! { diff --git a/src/show.rs b/src/show.rs index 2d2d8f1..78aceef 100644 --- a/src/show.rs +++ b/src/show.rs @@ -101,10 +101,10 @@ impl<'a, 'b> Printer<'a, 'b> { #[allow(warnings)] mod tests { use {Value, String, Integer, Float, Boolean, Datetime, Array, Table}; - use std::collections::HashMap; + use std::collections::TreeMap; macro_rules! map( ($($k:expr: $v:expr),*) => ({ - let mut _m = HashMap::new(); + let mut _m = TreeMap::new(); $(_m.insert($k.to_string(), $v);)* _m }) ) diff --git a/src/toml.rs b/src/toml.rs index 440da6a..7e5908d 100644 --- a/src/toml.rs +++ b/src/toml.rs @@ -43,7 +43,7 @@ extern crate serialize; -use std::collections::HashMap; +use std::collections::TreeMap; use std::from_str::FromStr; pub use parser::{Parser, Error}; @@ -70,7 +70,7 @@ pub enum Value { } pub type Array = Vec; -pub type Table = HashMap; +pub type Table = TreeMap; impl Value { /// Tests whether this and another value have the same type. @@ -176,7 +176,7 @@ impl Value { for key in path.split('.') { match cur_value { &Table(ref hm) => { - match hm.find_equiv(&key) { + match hm.find_with(|k| key.cmp(&k.as_slice())) { Some(v) => cur_value = v, _ => return None } -- cgit v1.2.3