From 0ef99a62a40c2b0b11b0848a558c65419795a9e1 Mon Sep 17 00:00:00 2001 From: NikVolf Date: Sat, 29 Dec 2018 23:32:36 +0300 Subject: [PATCH] refactor to const --- Cargo.lock | 4 ---- storage/src/tree_state.rs | 24 ++++++++---------------- 2 files changed, 8 insertions(+), 20 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 156a6d72..ca70a4d3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1680,10 +1680,6 @@ name = "yaml-rust" version = "0.3.5" source = "registry+https://github.com/rust-lang/crates.io-index" -[[patch.unused]] -name = "rust-crypto" -version = "0.2.36" - [metadata] "checksum abstract-ns 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "2f451afbdf8ed8c8f8a98433055bb9a6b7a72aef4baff16227d2a43dd547f43b" "checksum aho-corasick 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)" = "500909c4f87a9e52355b26626d890833e9e1d53ac566db76c36faa984b889699" diff --git a/storage/src/tree_state.rs b/storage/src/tree_state.rs index 53c72029..7051a8a7 100644 --- a/storage/src/tree_state.rs +++ b/storage/src/tree_state.rs @@ -73,15 +73,13 @@ lazy_static! { } pub trait Dim { - fn height() -> u32; + const HEIGHT: usize; } pub struct H32; impl Dim for H32 { - fn height() -> u32 { - 32 - } + const HEIGHT: usize = 32; } pub struct TreeState { @@ -97,7 +95,7 @@ impl TreeState { _phantom: ::std::marker::PhantomData, left: None, right: None, - parents: vec![None; D::height() as usize - 1], + parents: vec![None; D::HEIGHT - 1], } } @@ -113,7 +111,7 @@ impl TreeState { .expect("none variant is handled in the branch above; qed"); let mut combined = sha256_compress(&*former_left, &*former_right); - for i in 0..D::height()-1 { + for i in 0..D::HEIGHT-1 { let parent_slot = self.parents.get_mut(i as usize) .expect("Vector is at least self.height in size"); @@ -137,7 +135,7 @@ impl TreeState { let mut root = sha256_compress(&**left, &**right); - for i in 0..D::height()-1 { + for i in 0..D::HEIGHT-1 { match &self.parents[i as usize] { &Some(ref parent) => { root = sha256_compress(&**parent, &*root); } &None => { root = sha256_compress(&*root, &*EMPTY_ROOTS[i as usize + 1]); }, @@ -179,9 +177,7 @@ mod tests { pub struct H4; impl Dim for H4 { - fn height() -> u32 { - 4 - } + const HEIGHT: usize = 4; } type TestTreeState = TreeState

; @@ -189,17 +185,13 @@ mod tests { pub struct H1; impl Dim for H1 { - fn height() -> u32 { - 1 - } + const HEIGHT: usize = 1; } pub struct H2; impl Dim for H2 { - fn height() -> u32 { - 2 - } + const HEIGHT: usize = 2; } #[test]