Simplify code for WireHgId

Summary:
We already have a macro to make it easier to create wire representation of hash types, let's use it on `HgId` to reduce copy-pasting.

Changes:
- Added `Ord` implementations to wire hash types, as `WireHgId` used it.
- Added from/into implementations on `HgId` to byte arrays, which were used by the macro.
- Changed Debug implementation so it prints hex instead of an actual array of bytes

Reviewed By: krallin

Differential Revision: D30933067

fbshipit-source-id: c88911bfc91e44e07f2f658098036b766495d05f
This commit is contained in:
Yan Soares Couto 2021-09-20 05:25:44 -07:00 committed by Facebook GitHub Bot
parent f1cb23d4e1
commit 7725ca63b9
3 changed files with 24 additions and 73 deletions

View File

@ -12,7 +12,7 @@ macro_rules! wire_hash {
size => $size: literal,
} => {
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[derive(Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord)]
pub struct $wire([u8; $wire::len()]);
impl $wire {
@ -21,6 +21,12 @@ macro_rules! wire_hash {
}
}
impl std::fmt::Debug for $wire {
fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(fmt, "{}({:?})", stringify!($wire), types::sha::to_hex(&self.0))
}
}
impl ToWire for $api {
type Wire = $wire;

View File

@ -99,7 +99,6 @@ use std::fmt;
#[cfg(any(test, feature = "for-tests"))]
use quickcheck::Arbitrary;
use serde::{self, de::Error, Deserializer, Serializer};
use serde_derive::{Deserialize, Serialize};
use thiserror::Error;
@ -278,76 +277,17 @@ impl ToApi for WireEdenApiServerError {
}
}
#[derive(Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord)]
pub struct WireHgId([u8; WireHgId::len()]);
impl WireHgId {
const fn len() -> usize {
20
}
}
impl ToWire for HgId {
type Wire = WireHgId;
fn to_wire(self) -> Self::Wire {
WireHgId(self.into_byte_array())
}
}
impl ToApi for WireHgId {
type Api = HgId;
type Error = Infallible;
fn to_api(self) -> Result<Self::Api, Self::Error> {
Ok(HgId::from_byte_array(self.0))
}
wire_hash! {
wire => WireHgId,
api => HgId,
size => 20,
}
impl fmt::Display for WireHgId {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
match self.to_api() {
Ok(api) => fmt::Display::fmt(&api, fmt),
Err(_) => Err(fmt::Error),
}
}
}
impl fmt::Debug for WireHgId {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
match self.to_api() {
Ok(api) => write!(fmt, "WireHgId({:?})", &api.to_hex()),
Err(_) => Err(fmt::Error),
}
}
}
impl serde::Serialize for WireHgId {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_bytes(&self.0)
}
}
impl<'de> serde::Deserialize<'de> for WireHgId {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let bytes: serde_bytes::ByteBuf = serde_bytes::deserialize(deserializer)?;
let bytes = bytes.as_ref();
if bytes.len() == Self::len() {
let mut ary = [0u8; Self::len()];
ary.copy_from_slice(&bytes);
Ok(WireHgId(ary))
} else {
Err(D::Error::custom(TryFromBytesError {
expected_len: Self::len(),
found_len: bytes.len(),
}))
Err(e) => match e {},
}
}
}
@ -516,13 +456,6 @@ fn is_default<T: Default + PartialEq>(v: &T) -> bool {
v == &T::default()
}
#[cfg(any(test, feature = "for-tests"))]
impl Arbitrary for WireHgId {
fn arbitrary<G: quickcheck::Gen>(g: &mut G) -> Self {
HgId::arbitrary(g).to_wire()
}
}
#[cfg(any(test, feature = "for-tests"))]
impl Arbitrary for WireRepoPathBuf {
fn arbitrary<G: quickcheck::Gen>(g: &mut G) -> Self {

View File

@ -206,6 +206,18 @@ impl HgId {
}
}
impl From<[u8; HgId::len()]> for HgId {
fn from(bytes: [u8; HgId::len()]) -> Self {
Self::from_byte_array(bytes)
}
}
impl From<HgId> for [u8; HgId::len()] {
fn from(id: HgId) -> Self {
id.into_byte_array()
}
}
impl Display for HgId {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
Display::fmt(&self.to_hex(), fmt)