Migrate to a TreeMap for determinism

This commit is contained in:
Alex Crichton 2014-07-15 17:30:00 -07:00
parent bec6b768ec
commit b4a4ed72d7
4 changed files with 26 additions and 26 deletions

View file

@ -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<Table> {
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) => {

View file

@ -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: serialize::Encodable<Encoder, Error>>(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<DecodeError> 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<DecodeError> 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<String, int>,
map: TreeMap<String, int>,
set: HashSet<char>,
}
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<String, String> }
struct Foo { a: TreeMap<String, String> }
let v = Foo { a: map! { a: "foo".to_string() } };
let mut d = Decoder::new(Table(map! {

View file

@ -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
}) )

View file

@ -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<Value>;
pub type Table = HashMap<String, Value>;
pub type Table = TreeMap<String, Value>;
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
}