Use AsRef everywhere instead of Into + remove unneeded Clone bounds on T (#21)

* Replace Into<Vec<u8>> bound on T with AsRef<[u8]>.

* Remove unneeded Clone bounds on T.
This commit is contained in:
Romain Ruetschi
2016-12-12 18:29:54 +01:00
committed by Frederic Jacobs
parent 90d804e3ce
commit 46ee60a748
5 changed files with 24 additions and 18 deletions

View File

@ -6,7 +6,7 @@ use ring::digest::{ Algorithm, Context, digest, Digest };
pub trait HashUtils {
/// Compute the hash the given byte array
fn hash_bytes(&'static self, bytes: &[u8]) -> Digest;
fn hash_bytes(&'static self, bytes: &AsRef<[u8]>) -> Digest;
/// Compute the hash of the concatenation of `left` and `right`.
// XXX: This is overly generic temporarily to make refactoring easier.
@ -16,8 +16,8 @@ pub trait HashUtils {
impl HashUtils for Algorithm {
fn hash_bytes(&'static self, bytes: &[u8]) -> Digest {
digest(self, bytes)
fn hash_bytes(&'static self, bytes: &AsRef<[u8]>) -> Digest {
digest(self, bytes.as_ref())
}
fn combine_hashes(&'static self, left: &AsRef<[u8]>, right: &AsRef<[u8]>) -> Digest {

View File

@ -24,11 +24,13 @@ pub struct MerkleTree<T> {
count: usize
}
impl <T> MerkleTree<T> where T: Into<Vec<u8>> + Clone {
impl <T> MerkleTree<T> {
/// Constructs a Merkle Tree from a vector of data blocks.
/// Returns `None` if `values` is empty.
pub fn from_vec(algorithm: &'static Algorithm, values: Vec<T>) -> Option<Self> {
pub fn from_vec(algorithm: &'static Algorithm, values: Vec<T>) -> Option<Self>
where T: AsRef<[u8]> {
if values.is_empty() {
return None
}
@ -101,9 +103,11 @@ impl <T> MerkleTree<T> where T: Into<Vec<u8>> + Clone {
/// Generate an inclusion proof for the given value.
/// Returns `None` if the given value is not found in the tree.
pub fn gen_proof(&self, value: T) -> Option<Proof<T>> {
pub fn gen_proof(&self, value: T) -> Option<Proof<T>>
where T: AsRef<[u8]> {
let root_hash = self.root_hash().clone();
let node_hash = self.algorithm.hash_bytes(&value.clone().into());
let node_hash = self.algorithm.hash_bytes(&value.as_ref());
Lemma::new(&self.root, node_hash.as_ref()).map(|lemma|
Proof::new(self.algorithm, root_hash, lemma, value)

View File

@ -88,7 +88,7 @@ impl Lemma {
/// Attempts to generate a proof that the a value with hash `needle` is a member of the given `tree`.
pub fn new<T>(tree: &Tree<T>, needle: &[u8]) -> Option<Lemma>
where T: Into<Vec<u8>> + Clone {
where T: AsRef<[u8]> {
match *tree {
Tree::Leaf { ref hash, .. } =>
@ -112,7 +112,7 @@ impl Lemma {
}
fn new_tree_proof<T>(hash: &[u8], needle: &[u8], left: &Tree<T>, right: &Tree<T>) -> Option<Lemma>
where T: Into<Vec<u8>> + Clone {
where T: AsRef<[u8]> {
Lemma::new(left, needle)
.map(|lemma| {

View File

@ -21,7 +21,7 @@ impl <T> Proof<T> {
/// Encode this `Proof` to its Protobuf representation.
pub fn into_protobuf(self) -> ProofProto
where T: Into<Vec<u8>>
where T: AsRef<[u8]>
{
ProofProto::from_proof(self)
}
@ -34,7 +34,9 @@ impl <T> Proof<T> {
}
/// Serialize this `Proof` with Protobuf.
pub fn write_to_bytes(self) -> ProtobufResult<Vec<u8>> where T: Into<Vec<u8>> {
pub fn write_to_bytes(self) -> ProtobufResult<Vec<u8>>
where T: AsRef<[u8]> {
self.into_protobuf().write_to_bytes()
}
@ -43,15 +45,15 @@ impl <T> Proof<T> {
impl ProofProto {
pub fn from_proof<T>(proof: Proof<T>) -> Self
where T: Into<Vec<u8>>
{
where T: AsRef<[u8]> {
let mut proto = Self::new();
match proof {
Proof { root_hash, lemma, value, .. } => {
proto.set_root_hash(root_hash);
proto.set_lemma(LemmaProto::from_lemma(lemma));
proto.set_value(value.into());
proto.set_value(value.as_ref().into());
}
}
@ -59,8 +61,8 @@ impl ProofProto {
}
pub fn into_proof<T>(mut self, algorithm: &'static Algorithm) -> Option<Proof<T>>
where T: From<Vec<u8>>
{
where T: From<Vec<u8>> {
if !self.has_root_hash() || !self.has_lemma() {
return None;
}

View File

@ -36,9 +36,9 @@ impl <T> Tree<T> {
/// Create a new leaf
pub fn make_leaf(algo: &'static Algorithm, value: T) -> Tree<T>
where T: Into<Vec<u8>> + Clone {
where T: AsRef<[u8]> {
let hash = algo.hash_bytes(&value.clone().into());
let hash = algo.hash_bytes(&value.as_ref());
Tree::new(hash, value)
}