feat(cache) WasmHash becomes Hash.

To create a `WasmHash`, we need access to the bytes of a Wasm module,
which isn't possible with the new API as far as I know. So `WasmHash`
becomes `Hash` and can, semantically, represent any kind of key for
the cache.
This commit is contained in:
Ivan Enderlin
2020-07-10 16:15:52 +02:00
parent efde45c697
commit 917f879a4b

27
lib/cache/src/hash.rs vendored
View File

@@ -2,26 +2,21 @@ use crate::DeserializeError;
use std::str::FromStr; use std::str::FromStr;
use std::string::ToString; use std::string::ToString;
/// The hash of a wasm module. /// A hash used as a key when loading and storing modules in a
/// /// [`Cache`].
/// Used as a key when loading and storing modules in a [`Cache`].
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
// WasmHash is made up of a 32 byte array // Hash is made up of a 32 byte array
pub struct WasmHash([u8; 32]); pub struct Hash([u8; 32]);
impl WasmHash { impl Hash {
/// Creates a new hash. Has to be encodable as a Hex format. /// Creates a new hash. Has to be encodable as a hex format.
pub fn new(bytes: [u8; 32]) -> Self { pub fn new(bytes: [u8; 32]) -> Self {
Self(bytes) Self(bytes)
} }
/// Hash a wasm module. /// Creates a new hash from a slice of bytes.
/// pub fn generate(bytes: &[u8]) -> Self {
/// # Note: let hash = blake3::hash(bytes);
/// This does no verification that the supplied data
/// is, in fact, a wasm module.
pub fn generate(wasm: &[u8]) -> Self {
let hash = blake3::hash(wasm);
Self::new(hash.into()) Self::new(hash.into())
} }
@@ -32,7 +27,7 @@ impl WasmHash {
} }
} }
impl ToString for WasmHash { impl ToString for Hash {
/// Create the hexadecimal representation of the /// Create the hexadecimal representation of the
/// stored hash. /// stored hash.
fn to_string(&self) -> String { fn to_string(&self) -> String {
@@ -40,7 +35,7 @@ impl ToString for WasmHash {
} }
} }
impl FromStr for WasmHash { impl FromStr for Hash {
type Err = DeserializeError; type Err = DeserializeError;
/// Create hash from hexadecimal representation /// Create hash from hexadecimal representation
fn from_str(s: &str) -> Result<Self, Self::Err> { fn from_str(s: &str) -> Result<Self, Self::Err> {