Skip to content

Commit

Permalink
Hide faster_hex behind hex_utils
Browse files Browse the repository at this point in the history
  • Loading branch information
Veykril committed Sep 26, 2024
1 parent a350321 commit ca3ecf1
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 17 deletions.
9 changes: 2 additions & 7 deletions lib/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use futures::stream::BoxStream;
use thiserror::Error;

use crate::content_hash::ContentHash;
use crate::hex_util;
use crate::hex_util::to_reverse_hex_digit;
use crate::index::Index;
use crate::merge::Merge;
Expand Down Expand Up @@ -53,13 +54,7 @@ id_type!(pub ConflictId);

impl ChangeId {
pub fn reverse_hex(&self) -> String {
let mut buffer = vec![0; self.0.len() * 2];
faster_hex::hex_encode(&self.0, &mut buffer).unwrap();
buffer
.iter_mut()
.for_each(|b| *b = to_reverse_hex_digit(*b));

String::from_utf8(buffer).unwrap()
hex_util::encode_hex_string_reverse(&self.0)
}
}

Expand Down
6 changes: 4 additions & 2 deletions lib/src/content_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ mod tests {
use std::collections::BTreeMap;
use std::collections::HashMap;

use crate::hex_util;

use super::*;

#[test]
Expand Down Expand Up @@ -215,7 +217,7 @@ mod tests {
x: Vec<Option<i32>>,
y: i64,
}
let foo_hash = faster_hex::hex_string(&hash(&Foo {
let foo_hash = hex_util::encode_hex_string(&hash(&Foo {
x: vec![None, Some(42)],
y: 17,
}));
Expand All @@ -231,7 +233,7 @@ mod tests {
y: Y,
}
assert_eq!(
faster_hex::hex_string(&hash(&GenericFoo {
hex_util::encode_hex_string(&hash(&GenericFoo {
x: vec![None, Some(42)],
y: 17i64
})),
Expand Down
3 changes: 2 additions & 1 deletion lib/src/default_index/mutable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ use crate::backend::ChangeId;
use crate::backend::CommitId;
use crate::commit::Commit;
use crate::file_util::persist_content_addressed_temp_file;
use crate::hex_util;
use crate::index::AllHeadsForGcUnsupported;
use crate::index::ChangeIdIndex;
use crate::index::Index;
Expand Down Expand Up @@ -359,7 +360,7 @@ impl MutableIndexSegment {
self.serialize_local_entries(&mut buf);
let mut hasher = Blake2b512::new();
hasher.update(&buf);
let index_file_id_hex = faster_hex::hex_string(&hasher.finalize());
let index_file_id_hex = hex_util::encode_hex_string(&hasher.finalize());
let index_file_path = dir.join(&index_file_id_hex);

let mut temp_file = NamedTempFile::new_in(dir)?;
Expand Down
3 changes: 2 additions & 1 deletion lib/src/git_backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1517,6 +1517,7 @@ mod tests {

use super::*;
use crate::content_hash::blake2b_hash;
use crate::hex_util;

#[test_case(false; "legacy tree format")]
#[test_case(true; "tree-level conflict format")]
Expand Down Expand Up @@ -2132,7 +2133,7 @@ mod tests {
};

let mut signer = |data: &_| {
let hash: String = faster_hex::hex_string(&blake2b_hash(data));
let hash: String = hex_util::encode_hex_string(&blake2b_hash(data));
Ok(format!("test sig\n\n\nhash={hash}").into_bytes())
};

Expand Down
14 changes: 14 additions & 0 deletions lib/src/hex_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,20 @@ pub fn common_hex_len(bytes_a: &[u8], bytes_b: &[u8]) -> usize {
.unwrap_or_else(|| bytes_a.len().min(bytes_b.len()) * 2)
}

pub fn encode_hex_string_reverse(src: &[u8]) -> String {
let mut buffer = vec![0; src.len() * 2];
faster_hex::hex_encode(src, &mut buffer).unwrap();
buffer
.iter_mut()
.for_each(|b| *b = to_reverse_hex_digit(*b));

String::from_utf8(buffer).unwrap()
}

pub fn encode_hex_string(src: &[u8]) -> String {
faster_hex::hex_string(src)
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
4 changes: 2 additions & 2 deletions lib/src/object_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ macro_rules! impl_id_type {
pub(crate) use id_type;
pub(crate) use impl_id_type;

use crate::hex_util::decode_hex_string;
use crate::hex_util::{self, decode_hex_string};

/// An identifier prefix (typically from a type implementing the [`ObjectId`]
/// trait) with facilities for converting between bytes and a hex string.
Expand Down Expand Up @@ -141,7 +141,7 @@ impl HexPrefix {
}

pub fn hex(&self) -> String {
let mut hex_string = faster_hex::hex_string(&self.min_prefix_bytes);
let mut hex_string = hex_util::encode_hex_string(&self.min_prefix_bytes);
if self.has_odd_byte {
hex_string.pop().unwrap();
}
Expand Down
3 changes: 2 additions & 1 deletion lib/src/stacked_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ use tempfile::NamedTempFile;
use thiserror::Error;

use crate::file_util::persist_content_addressed_temp_file;
use crate::hex_util;
use crate::lock::FileLock;

pub trait TableSegment {
Expand Down Expand Up @@ -333,7 +334,7 @@ impl MutableTable {
let buf = self.maybe_squash_with_ancestors().serialize();
let mut hasher = Blake2b512::new();
hasher.update(&buf);
let file_id_hex = faster_hex::hex_string(&hasher.finalize());
let file_id_hex = hex_util::encode_hex_string(&hasher.finalize());
let file_path = store.dir.join(&file_id_hex);

let mut temp_file = NamedTempFile::new_in(&store.dir)?;
Expand Down
5 changes: 3 additions & 2 deletions lib/tests/test_git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ use jj_lib::git::GitRefUpdate;
use jj_lib::git::RefName;
use jj_lib::git::SubmoduleConfig;
use jj_lib::git_backend::GitBackend;
use jj_lib::hex_util;
use jj_lib::object_id::ObjectId;
use jj_lib::op_store::BookmarkTarget;
use jj_lib::op_store::RefTarget;
Expand Down Expand Up @@ -1381,8 +1382,8 @@ fn test_import_refs_missing_git_commit() {

let commit1 = empty_git_commit(&git_repo, "refs/heads/main", &[]);
let commit2 = empty_git_commit(&git_repo, "refs/heads/main", &[&commit1]);
let shard = faster_hex::hex_string(&commit1.id().as_bytes()[..1]);
let object_basename = faster_hex::hex_string(&commit1.id().as_bytes()[1..]);
let shard = hex_util::encode_hex_string(&commit1.id().as_bytes()[..1]);
let object_basename = hex_util::encode_hex_string(&commit1.id().as_bytes()[1..]);
let object_store_path = git_repo.path().join("objects");
let object_file = object_store_path.join(&shard).join(object_basename);
let backup_object_file = object_store_path.join(&shard).join("backup");
Expand Down
3 changes: 2 additions & 1 deletion lib/testutils/src/test_signing_backend.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use jj_lib::content_hash::blake2b_hash;
use jj_lib::hex_util;
use jj_lib::signing::SigStatus;
use jj_lib::signing::SignError;
use jj_lib::signing::SignResult;
Expand All @@ -25,7 +26,7 @@ impl SigningBackend for TestSigningBackend {
body.extend_from_slice(key.as_bytes());
body.extend_from_slice(data);

let hash: String = faster_hex::hex_string(&blake2b_hash(&body));
let hash: String = hex_util::encode_hex_string(&blake2b_hash(&body));

Ok(format!("{PREFIX}{key}\n{hash}").into_bytes())
}
Expand Down

0 comments on commit ca3ecf1

Please sign in to comment.