Git Product home page Git Product logo

type_hash's Introduction

type_hash

Generate a hash for a Rust type.

The primary use-case for this crate is for detecting differences in message types between versions of a crate.

The TypeHash trait is implemented for most built-in types and a derive macro is provided, for implementing it for your own types.

Examples

use type_hash::TypeHash;

#[derive(TypeHash)]
pub enum Message {
    LaunchMissiles { destination: String },
    CancelMissiles,
}

fn main() {
    let hash = Message::type_hash();
    // this will only change if the type definition changes
    assert_eq!(hash, 11652809455620829461);
}

Customising derived TypeHash implementations

#[type_hash(foreign_type)]

If a struct field has a foreign type that does not implement TypeHash, you can mark it as a foreign type and the derive TypeHash implementation will use the name of the type in the hash instead. You need to be a little bit careful here because a change in the third party crate could change your type in an undetectable way.

#[derive(TypeHash)]
pub struct MyStruct {
    #[type_hash(foreign_type)]
    data: ArrayVec<[u16; 7]>
}

#[type_hash(skip)]

Skip a field, so it is not part of the hash.

#[derive(TypeHash)]
pub struct MyStruct {
    #[type_hash(skip)]
    not_important: Vec<i64>,
}

#[type_hash(as = "...")]

Hash a field as if it had a different type. This allows you to change the type of a field to a different type that is still compatible for your application, without affecting the hash.

#[derive(TypeHash)]
pub struct MyStruct {
    #[type_hash(as = "HashSet<i64>")]
    numbers: BTreeSet<i64>,
}

type_hash's People

Contributors

peterjoel avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

type_hash's Issues

Stack overflow with recursive types

Any recursive type will cause a stack overflow.

e.g.

#[derive(TypeHash)]
struct Recursive {
    foo: Option<Box<Recursive>>,
}

Produces:

thread 'recursive_generic_type_terminates' has overflowed its stack
fatal runtime error: stack overflow

That simple example could be detected in the macro, but it can't in the general case. e.g. mutually recursive types:

#[derive(TypeHash)]
struct A {
    foo: Option<Box<B>>,
}

#[derive(TypeHash)]
struct B {
    foo: Option<Box<A>>,
}

`A<T>` and `A<U>` get the same hash

I realized this while writing basically the same functionality, just compile time instead of runtime. If this is a problem, you'll have to include the typehash of T in A<T>'s hash.

BTW, your type_hash function would be a lot simpler if you hashed module_path!() and the name of the type. That should be enough to uniquely identify a type.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.