diff options
author | David Sherret <dsherret@users.noreply.github.com> | 2022-07-19 11:58:18 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-07-19 11:58:18 -0400 |
commit | 0ab262b901348e9251262a02bef17d14ed13b997 (patch) | |
tree | fc5a6e3926ea7480714cbc844098eca6c43c1ab5 /cli/cache/common.rs | |
parent | e99d64acedb6e111d33f53599da494865978f1aa (diff) |
feat: emit files on demand and fix racy emit (#15220)
Diffstat (limited to 'cli/cache/common.rs')
-rw-r--r-- | cli/cache/common.rs | 35 |
1 files changed, 28 insertions, 7 deletions
diff --git a/cli/cache/common.rs b/cli/cache/common.rs index c01c1ab9a..b536d6cb2 100644 --- a/cli/cache/common.rs +++ b/cli/cache/common.rs @@ -1,16 +1,37 @@ // Copyright 2018-2022 the Deno authors. All rights reserved. MIT license. +use std::hash::Hasher; + use deno_core::error::AnyError; use deno_runtime::deno_webstorage::rusqlite::Connection; -/// Very fast non-cryptographically secure hash. -pub fn fast_insecure_hash(bytes: &[u8]) -> u64 { - use std::hash::Hasher; - use twox_hash::XxHash64; +/// A very fast insecure hasher that uses the xxHash algorithm. +#[derive(Default)] +pub struct FastInsecureHasher(twox_hash::XxHash64); + +impl FastInsecureHasher { + pub fn new() -> Self { + Self::default() + } + + pub fn write_str(&mut self, text: &str) -> &mut Self { + self.write(text.as_bytes()); + self + } + + pub fn write(&mut self, bytes: &[u8]) -> &mut Self { + self.0.write(bytes); + self + } + + pub fn write_u64(&mut self, value: u64) -> &mut Self { + self.0.write_u64(value); + self + } - let mut hasher = XxHash64::default(); - hasher.write(bytes); - hasher.finish() + pub fn finish(&self) -> u64 { + self.0.finish() + } } /// Runs the common sqlite pragma. |