From 7ca3670338e48bdbb99d2df770056ce57f63f5b8 Mon Sep 17 00:00:00 2001 From: Lucien Greathouse Date: Tue, 12 Mar 2019 16:23:21 -0700 Subject: [PATCH] Make Imfs ordering deterministic by using BTreeSet --- server/src/imfs.rs | 43 ++++++++++++++++++++++++++++++++++--------- server/tests/imfs.rs | 8 ++++---- 2 files changed, 38 insertions(+), 13 deletions(-) diff --git a/server/src/imfs.rs b/server/src/imfs.rs index c144ae5e..0672ddb1 100644 --- a/server/src/imfs.rs +++ b/server/src/imfs.rs @@ -1,9 +1,10 @@ use std::{ - collections::{HashMap, HashSet}, - path::{self, Path, PathBuf}, + cmp::Ordering, + collections::{HashMap, HashSet, BTreeSet}, fmt, fs, io, + path::{self, Path, PathBuf}, }; use failure::Fail; @@ -237,7 +238,7 @@ impl Imfs { } else if metadata.is_dir() { let item = ImfsItem::Directory(ImfsDirectory { path: path.to_path_buf(), - children: HashSet::new(), + children: BTreeSet::new(), }); self.items.insert(path.to_path_buf(), item); @@ -285,19 +286,43 @@ impl Imfs { } } -#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] pub struct ImfsFile { pub path: PathBuf, pub contents: Vec, } -#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] -pub struct ImfsDirectory { - pub path: PathBuf, - pub children: HashSet, +impl PartialOrd for ImfsFile { + fn partial_cmp(&self, other: &Self) -> Option { + Some(self.cmp(other)) + } } -#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +impl Ord for ImfsFile { + fn cmp(&self, other: &Self) -> Ordering { + self.path.cmp(&other.path) + } +} + +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +pub struct ImfsDirectory { + pub path: PathBuf, + pub children: BTreeSet, +} + +impl PartialOrd for ImfsDirectory { + fn partial_cmp(&self, other: &Self) -> Option { + Some(self.cmp(other)) + } +} + +impl Ord for ImfsDirectory { + fn cmp(&self, other: &Self) -> Ordering { + self.path.cmp(&other.path) + } +} + +#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)] pub enum ImfsItem { File(ImfsFile), Directory(ImfsDirectory), diff --git a/server/tests/imfs.rs b/server/tests/imfs.rs index 617ce06a..6dc3f49d 100644 --- a/server/tests/imfs.rs +++ b/server/tests/imfs.rs @@ -1,5 +1,5 @@ use std::{ - collections::{HashMap, HashSet}, + collections::{HashMap, HashSet, BTreeSet}, fs, path::PathBuf, }; @@ -80,7 +80,7 @@ fn base_tree() -> Result<(TempDir, Imfs, ExpectedImfs, TestResources), Error> { expected_roots.insert(root.path().to_path_buf()); let root_item = { - let mut children = HashSet::new(); + let mut children = BTreeSet::new(); children.insert(foo_path.clone()); children.insert(bar_path.clone()); @@ -91,7 +91,7 @@ fn base_tree() -> Result<(TempDir, Imfs, ExpectedImfs, TestResources), Error> { }; let foo_item = { - let mut children = HashSet::new(); + let mut children = BTreeSet::new(); children.insert(baz_path.clone()); ImfsItem::Directory(ImfsDirectory { @@ -199,7 +199,7 @@ fn adding_folder() -> Result<(), Error> { } let folder_item = { - let mut children = HashSet::new(); + let mut children = BTreeSet::new(); children.insert(file1_path.clone()); children.insert(file2_path.clone());