tysh-derive 0.1.0

This crate provides Tysh's derive macro
Documentation
  • Coverage
  • 50%
    1 out of 2 items documented1 out of 1 items with examples
  • Size
  • Source code size: 8.09 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 279 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 4s Average build duration of successful builds.
  • all releases: 4s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • eduidl/tysh
    9 0 1
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • eduidl

tysh

Tysh is th crate that helps with hashing the metadata of a type.

Purpose

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.

How to use

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);
    }
}