#[derive(Debug, Copy, Clone, PartialEq, Eq)]
struct MyStruct([u8; 20]);
impl AsRef<[u8; 20]> for MyStruct {
fn as_ref(&self) -> &[u8; 20] {
&self.0
}
}
impl kaspa_utils::hex::FromHex for MyStruct {
type Error = faster_hex::Error;
fn from_hex(hex_str: &str) -> Result<Self, Self::Error> {
let mut bytes = [0u8; 20];
faster_hex::hex_decode(hex_str.as_bytes(), &mut bytes)?;
Ok(MyStruct(bytes))
}
}
impl From<[u8; 20]> for MyStruct {
fn from(value: [u8; 20]) -> Self {
MyStruct(value)
}
}
kaspa_utils::serde_impl_ser_fixed_bytes_ref!(MyStruct, 20);
kaspa_utils::serde_impl_deser_fixed_bytes_ref!(MyStruct, 20);
let test_struct = MyStruct([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]);
let expected_str = r#""000102030405060708090a0b0c0d0e0f10111213""#;
let json = serde_json::to_string(&test_struct).unwrap();
assert_eq!(expected_str, json);
let from_json: MyStruct = serde_json::from_str(&json).unwrap();
assert_eq!(test_struct, from_json);
let encoded = bincode::serialize(&test_struct).unwrap();
assert_eq!(encoded, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]);
let decoded: MyStruct = bincode::deserialize(&encoded).unwrap();
assert_eq!(test_struct, decoded);
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
struct MyStruct([u8; 20]);
impl AsRef<[u8; 20]> for MyStruct {
fn as_ref(&self) -> &[u8; 20] {
&self.0
}
}
impl kaspa_utils::hex::FromHex for MyStruct {
type Error = faster_hex::Error;
fn from_hex(hex_str: &str) -> Result<Self, Self::Error> {
let mut bytes = [0u8; 20];
faster_hex::hex_decode(hex_str.as_bytes(), &mut bytes)?;
Ok(MyStruct(bytes))
}
}
impl From<[u8; 20]> for MyStruct {
fn from(value: [u8; 20]) -> Self {
MyStruct(value)
}
}
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
struct TestStruct {
#[serde(with = "kaspa_utils::serde_bytes_fixed_ref")]
v: MyStruct,
}
let test_struct = TestStruct { v: MyStruct([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]) };
let encoded = bincode::serialize(&test_struct).unwrap();
assert_eq!(encoded, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]);
let decoded: TestStruct = bincode::deserialize(&encoded).unwrap();
assert_eq!(test_struct, decoded);
let expected_str = r#"{"v":"000102030405060708090a0b0c0d0e0f10111213"}"#;
let json = serde_json::to_string(&test_struct).unwrap();
assert_eq!(expected_str, json);
let from_json: TestStruct = serde_json::from_str(&json).unwrap();
assert_eq!(test_struct, from_json);