Crates.io | tysh-derive |
lib.rs | tysh-derive |
version | 0.1.0 |
source | src |
created_at | 2023-03-25 12:14:03.116417 |
updated_at | 2023-03-25 12:14:03.116417 |
description | This crate provides Tysh's derive macro |
homepage | |
repository | https://github.com/eduidl/tysh |
max_upload_size | |
id | 820178 |
size | 7,447 |
Tysh is th crate that helps with hashing the metadata of a type.
Tysh is a tool that is meant to be used with the bincode crate, which helps with serializing data. The bincode crate has some compatibility issues with certain data structures, such as the Color { r: u8, g: u8, b: u8 }
and Color { b: u8, g: u8, r: u8 }
structures, even though they have the same type structure and are compatible with JSON serialization. This is because bincode can change the meaning of the data when serializing and deserializing it.
To avoid this problem, tysh also provides field names along with the type information, which helps to ensure compatibility between different versions of data structures.
use tysh::TypeHash;
#[derive(TypeHash)]
struct A {
a: u8,
b: u16,
}
This will generate the following code:
struct A {
a: u8,
b: u16,
}
impl ::tysh::TypeHash for A {
fn type_hash<H: ::core::hash::Hasher>(hasher: &mut H) {
use ::core::hash::Hash;
"@struct@".hash(hasher);
"A".hash(hasher);
"@field@".hash(hasher);
"a".hash(hasher);
<u8 as ::tysh::TypeHash>::type_hash(hasher);
"@field@".hash(hasher);
"b".hash(hasher);
<u16 as ::tysh::TypeHash>::type_hash(hasher);
}
}
You can also use the #[type_hash(name = "name")]
attribute to specify the internal name.
use tysh::TypeHash;
#[derive(TypeHash)]
#[type_hash(name = "AnotherName")]
struct A {
a: u8,
#[type_hash(name = "another special name")]
b: u16,
}
pub struct A {
a: u8,
b: u16,
}
impl ::tysh::TypeHash for A {
fn type_hash<H: ::core::hash::Hasher>(hasher: &mut H) {
use ::core::hash::Hash;
"@struct@".hash(hasher);
"AnotherName".hash(hasher);
"@field@".hash(hasher);
"a".hash(hasher);
<u8 as ::tysh::TypeHash>::type_hash(hasher);
"@field@".hash(hasher);
"another special name".hash(hasher);
<u16 as ::tysh::TypeHash>::type_hash(hasher);
}
}