object_id: make ObjectId constructors non-trait methods

I'm going to add try_from_hex(), which requires Self: Sized. Such trait bound
could be added, but I don't think we'll need abstracted ObjectId constructors
at all.
This commit is contained in:
Yuya Nishihara 2024-01-04 15:41:35 +09:00
parent 31b236a70d
commit 95d83cbfe5
9 changed files with 15 additions and 23 deletions

View file

@ -15,7 +15,6 @@ use std::path::Path;
use itertools::Itertools as _;
use jj_lib::backend::CommitId;
use jj_lib::object_id::ObjectId as _;
use crate::common::TestEnvironment;

View file

@ -1001,7 +1001,6 @@ mod tests {
use super::*;
use crate::backend::{ChangeId, CommitId};
use crate::default_index::DefaultMutableIndex;
use crate::object_id::ObjectId;
/// Generator of unique 16-byte ChangeId excluding root id
fn change_id_generator() -> impl FnMut() -> ChangeId {

View file

@ -446,7 +446,6 @@ fn unwrap_as_short_key<const N: usize>(key_bytes: &[u8]) -> &[u8; N] {
mod tests {
use super::*;
use crate::backend::ChangeId;
use crate::object_id::ObjectId;
#[derive(Clone, Copy, Eq, PartialEq)]
struct Position(usize);

View file

@ -15,12 +15,9 @@
#![allow(missing_docs)]
pub trait ObjectId {
fn new(value: Vec<u8>) -> Self;
fn object_type(&self) -> String;
fn from_bytes(bytes: &[u8]) -> Self;
fn as_bytes(&self) -> &[u8];
fn to_bytes(&self) -> Vec<u8>;
fn from_hex(hex: &str) -> Self;
fn hex(&self) -> String;
}
@ -36,6 +33,20 @@ macro_rules! id_type {
macro_rules! impl_id_type {
($name:ident) => {
impl $name {
pub fn new(value: Vec<u8>) -> Self {
Self(value)
}
pub fn from_bytes(bytes: &[u8]) -> Self {
Self(bytes.to_vec())
}
pub fn from_hex(hex: &str) -> Self {
Self(hex::decode(hex).unwrap())
}
}
impl std::fmt::Debug for $name {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
f.debug_tuple(stringify!($name)).field(&self.hex()).finish()
@ -43,10 +54,6 @@ macro_rules! impl_id_type {
}
impl crate::object_id::ObjectId for $name {
fn new(value: Vec<u8>) -> Self {
Self(value)
}
fn object_type(&self) -> String {
stringify!($name)
.strip_suffix("Id")
@ -55,10 +62,6 @@ macro_rules! impl_id_type {
.to_string()
}
fn from_bytes(bytes: &[u8]) -> Self {
Self(bytes.to_vec())
}
fn as_bytes(&self) -> &[u8] {
&self.0
}
@ -67,10 +70,6 @@ macro_rules! impl_id_type {
self.0.clone()
}
fn from_hex(hex: &str) -> Self {
Self(hex::decode(hex).unwrap())
}
fn hex(&self) -> String {
hex::encode(&self.0)
}

View file

@ -22,7 +22,7 @@ use std::sync::Arc;
use itertools::Itertools as _;
use thiserror::Error;
use crate::object_id::{HexPrefix, ObjectId as _};
use crate::object_id::HexPrefix;
use crate::op_heads_store::{OpHeadResolutionError, OpHeadsStore};
use crate::op_store::{OpStore, OpStoreError, OpStoreResult, OperationId};
use crate::operation::Operation;

View file

@ -205,7 +205,6 @@ pub fn classify_branch_push_action(targets: TrackingRefPair) -> BranchPushAction
#[cfg(test)]
mod tests {
use super::*;
use crate::object_id::ObjectId;
use crate::op_store::RemoteRefState;
fn new_remote_ref(target: RefTarget) -> RemoteRef {

View file

@ -24,7 +24,6 @@ use rand_chacha::ChaCha20Rng;
use crate::backend::{ChangeId, Commit, Signature, Timestamp};
use crate::fmt_util::binary_prefix;
use crate::fsmonitor::FsmonitorKind;
use crate::object_id::ObjectId;
use crate::signing::SignBehavior;
#[derive(Debug, Clone)]

View file

@ -15,7 +15,6 @@
use jj_lib::backend::{ChangeId, MillisSinceEpoch, Signature, Timestamp};
use jj_lib::matchers::EverythingMatcher;
use jj_lib::merged_tree::DiffSummary;
use jj_lib::object_id::ObjectId;
use jj_lib::repo::Repo;
use jj_lib::repo_path::{RepoPath, RepoPathBuf};
use jj_lib::settings::UserSettings;

View file

@ -31,7 +31,6 @@ use jj_lib::fsmonitor::FsmonitorKind;
use jj_lib::local_working_copy::LocalWorkingCopy;
use jj_lib::merge::Merge;
use jj_lib::merged_tree::MergedTreeBuilder;
use jj_lib::object_id::ObjectId;
use jj_lib::op_store::{OperationId, WorkspaceId};
use jj_lib::repo::{ReadonlyRepo, Repo};
use jj_lib::repo_path::{RepoPath, RepoPathBuf, RepoPathComponent};