This is part of Silx project
hashed-type-def-core
contains the core definition and implementations for deriving type hashcode
Content
The crate provides two traits HashedTypeDef
and HashedTypeUuid
- the
HashedTypeDef
trait is the basis for defining a type's hashcode
- the
HashedTypeUuid
trait provides additional methods, such as UUID translation of the hashcode
The regular way to implement HashedTypeDef
is by using the procedural macro #[derive(HashedTypeDef)]
The following example gives an overview of hashed-type-def
features.
Example of type hashcode implementation
Cargo.toml
[package]
name = "silx_hashed-type-def_examples"
version = "0.1.1"
edition = "2021"
[dependencies]
uuid = "1.7.0"
hashed-type-def = { version = "^0.1.1", features = ["derive"] }
main.rs
use std::fmt::Debug;
use hashed_type_def::{ HashedTypeDef, HashedTypeUuid, add_hashed_type_def_param, };
#[derive(Debug,HashedTypeDef)]
struct MyStruct<'a,'b,T,U: Debug,const C1: usize, const C2: bool> where T: Into<U> + 'b, 'a: 'b {
#[allow(dead_code)] un: &'a String,
#[allow(dead_code)] deux: T,
#[allow(dead_code)] trois: &'b U,
}
pub mod instance0 {
use hashed_type_def::HashedTypeDef;
use std::fmt::Debug;
#[derive(Debug,HashedTypeDef)]
pub struct MyStruct<'a,'b,T,U: Debug,const C1: usize, const C2: bool> where T: Into<U> + 'b, 'a: 'b {
#[allow(dead_code)] un: &'a String,
#[allow(dead_code)] deux: T,
#[allow(dead_code)] trois: &'b U,
}
}
pub mod instance1 {
use hashed_type_def::{ add_hashed_type_def, HashedTypeDef, };
use std::fmt::Debug;
pub struct MyStruct<'a,'b,T,U: Debug,const C1: usize, const C2: bool> where T: Into<U> + 'b, 'a: 'b {
#[allow(dead_code)] un: &'a String,
#[allow(dead_code)] deux: T,
#[allow(dead_code)] trois: &'b U,
}
add_hashed_type_def!{
pub struct MyStruct<'a,'b,T,U: Debug,const C1: usize, const C2: bool> where T: Into<U> + 'b, 'a: 'b {
un: &'a String,
deux: T,
trois: &'b U,
}
}
}
pub mod instance2 {
use hashed_type_def::HashedTypeDef;
use std::fmt::Debug;
#[derive(Debug,HashedTypeDef)]
pub struct MyStruct<'b,'a,T,U: Debug,const C1: usize, const C2: bool> where T: Into<U> + 'a, 'b: 'a {
#[allow(dead_code)] un: &'b String,
#[allow(dead_code)] deux: T,
#[allow(dead_code)] trois: &'a U,
}
}
pub mod instance3 {
use hashed_type_def::{ add_hashed_type_def, HashedTypeDef, };
use std::fmt::Debug;
pub struct MyStruct<'a,'b,T,U: Debug,const C1: usize, const C2: bool> where T: Into<U> + 'b, 'a: 'b {
#[allow(dead_code)] un: &'a String,
#[allow(dead_code)] deux: T,
#[allow(dead_code)] trois: &'b U,
}
add_hashed_type_def!{
pub struct MyStruct<'a,'b,T,U: Debug,const C1: usize, const C2: bool> where T: Into<U> + 'b, 'a: 'b {
instance3_MyStruct: (),
un: &'a String,
deux: T,
trois: &'b U,
}
}
}
pub mod instance4 {
use hashed_type_def::HashedTypeDef;
use std::fmt::Debug;
#[derive(Debug,HashedTypeDef)]
pub struct MyStruct<'a,'b,T,U,const C1: usize, const C2: bool> where 'a: 'b, T: Into<U> + 'b, U: Debug, {
#[allow(dead_code)] un: &'a String,
#[allow(dead_code)] deux: T,
#[allow(dead_code)] trois: &'b U,
}
}
pub mod instance5 {
use hashed_type_def::HashedTypeDef;
use std::fmt::Debug;
#[derive(Debug,HashedTypeDef)]
pub struct MyStruct<'a: 'b,'b, T: Into<U> + 'b, U: Debug, const C1: usize, const C2: bool> {
#[allow(dead_code)] un: &'a String,
#[allow(dead_code)] deux: T,
#[allow(dead_code)] trois: &'b U,
}
}
pub mod instance6 {
use hashed_type_def::HashedTypeDef;
use std::fmt::Debug;
#[derive(Debug,HashedTypeDef)]
pub struct MyStruct<'a,'b,T,U,const C1: usize, const C2: bool> where T: Into<U> + 'b, 'a: 'b {
#[allow(dead_code)] un: &'a String,
#[allow(dead_code)] deux: T,
#[allow(dead_code)] trois: &'b U,
}
}
pub mod instance7 {
use hashed_type_def::HashedTypeDef;
use std::fmt::Debug;
#[derive(Debug,HashedTypeDef)]
pub struct MyStruct<'a,'b,T,U: Debug,const C1: usize, const C2: bool> where T: Into<U> + 'b, {
#[allow(dead_code)] un: &'a String,
#[allow(dead_code)] deux: T,
#[allow(dead_code)] trois: &'b U,
}
}
fn fn_type_hash<'a: 'b,'b>(_tmp1: &'a (), _tmp2: &'b ()) -> uuid::Uuid { MyStruct::<'a,'b,f32,f64,12,true>::type_uuid() }
#[derive(Debug,HashedTypeDef)]
struct MyTupleStruct<T,U: Debug>(String,T,U,) where T: Into<U>;
#[derive(Debug,HashedTypeDef)]
struct MyEmptyStruct;
#[derive(Debug,HashedTypeDef)]
enum MyEnum<T,U: Debug> where T: Into<U> {
#[allow(dead_code)] Zero,
#[allow(dead_code)] Un(String, T,),
#[allow(dead_code)] Deux {
double: f64,
trois: U,
},
#[allow(dead_code)] Trois,
}
#[derive(Debug,HashedTypeDef)]
enum MyEmptyEnum { }
struct AnotherStruct<T: Debug> where T: Default { #[allow(dead_code)] t: T }
add_hashed_type_def_param!((Option<u32>,[u128;24]); struct AnotherStruct<T: Debug> where T: Default { t: T });
fn main() {
println!("MyStruct::<'static,'static,f32,f64,12,true>::type_uuid() -> {:x}", MyStruct::<'static,'static,f32,f64,12,true>::type_uuid());
println!("MyStruct::<'a,'b,f32,f64,12,true>::type_uuid() -> {:x}", { let a = (); { let b = (); fn_type_hash(&a,&b) } });
println!("MyStruct::<'static,'static,f32,f64,12,false>::type_uuid() -> {:x}", MyStruct::<'static,'static,f32,f64,12,false>::type_uuid());
println!("instance0::MyStruct::<'static,'static,f32,f64,12,false>::type_uuid() -> {:x}", instance0::MyStruct::<'static,'static,f32,f64,12,false>::type_uuid());
println!("instance1::MyStruct::<'static,'static,f32,f64,12,false>::type_uuid() -> {:x}", instance1::MyStruct::<'static,'static,f32,f64,12,false>::type_uuid());
println!("instance2::MyStruct::<'static,'static,f32,f64,12,false>::type_uuid() -> {:x}", instance2::MyStruct::<'static,'static,f32,f64,12,false>::type_uuid());
println!("instance3::MyStruct::<'static,'static,f32,f64,12,false>::type_uuid() -> {:x}", instance3::MyStruct::<'static,'static,f32,f64,12,false>::type_uuid());
println!("instance4::MyStruct::<'static,'static,f32,f64,12,false>::type_uuid() -> {:x}", instance4::MyStruct::<'static,'static,f32,f64,12,false>::type_uuid());
println!("instance5::MyStruct::<'static,'static,f32,f64,12,false>::type_uuid() -> {:x}", instance5::MyStruct::<'static,'static,f32,f64,12,false>::type_uuid());
println!("instance6::MyStruct::<'static,'static,f32,f64,12,false>::type_uuid() -> {:x}", instance6::MyStruct::<'static,'static,f32,f64,12,false>::type_uuid());
println!("instance7::MyStruct::<'static,'static,f32,f64,12,false>::type_uuid() -> {:x}", instance7::MyStruct::<'static,'static,f32,f64,12,false>::type_uuid());
println!("MyTupleStruct::<f32,f64>::type_uuid() -> {:x}", MyTupleStruct::<f32,f64>::type_uuid());
println!("MyEmptyStruct::type_uuid() -> {:x}", MyEmptyStruct::type_uuid());
println!("MyEnum::<f32,f64,>::type_uuid() -> {:x}", MyEnum::<f32,f64,>::type_uuid());
println!("MyEmptyEnum::type_uuid() -> {:x}", MyEmptyEnum::type_uuid());
println!("AnotherStruct::<Vec<u16>>::type_uuid() -> {:x}", AnotherStruct::<Vec<u16>>::type_uuid());
}
Typical output
MyStruct::<'static,'static,f32,f64,12,true>::type_uuid() -> bd84ac66-d0fa-5d1c-ae5e-25647a13dcb3
MyStruct::<'a,'b,f32,f64,12,true>::type_uuid() -> bd84ac66-d0fa-5d1c-ae5e-25647a13dcb3
MyStruct::<'static,'static,f32,f64,12,false>::type_uuid() -> 1a8a7365-1c9c-afed-cca3-983399a91fd8
instance0::MyStruct::<'static,'static,f32,f64,12,false>::type_uuid() -> 1a8a7365-1c9c-afed-cca3-983399a91fd8
instance1::MyStruct::<'static,'static,f32,f64,12,false>::type_uuid() -> 1a8a7365-1c9c-afed-cca3-983399a91fd8
instance2::MyStruct::<'static,'static,f32,f64,12,false>::type_uuid() -> bbe982ff-fcad-5390-86f0-cce2e7dbae6b
instance3::MyStruct::<'static,'static,f32,f64,12,false>::type_uuid() -> 56d4f1b7-af31-d361-3afb-dc89e52c2ded
instance4::MyStruct::<'static,'static,f32,f64,12,false>::type_uuid() -> 394096e5-5187-edf4-ac77-3f6edc052b72
instance5::MyStruct::<'static,'static,f32,f64,12,false>::type_uuid() -> e7e26201-4095-31d1-bfa3-fd4b62abc938
instance6::MyStruct::<'static,'static,f32,f64,12,false>::type_uuid() -> 0bee6197-ef3e-a446-890a-c34705c30cdd
instance7::MyStruct::<'static,'static,f32,f64,12,false>::type_uuid() -> f8f3fcc7-e4a5-e021-e200-763b4cf9df7a
MyTupleStruct::<f32,f64>::type_uuid() -> ddd04a26-6807-0f27-67b2-227db8f17b75
MyEmptyStruct::type_uuid() -> 4ede75e3-1bf7-5298-ae87-0ee1d57a1357
MyEnum::<f32,f64,>::type_uuid() -> fcad4ca3-6cd0-6c7e-21ea-658a12369d9f
MyEmptyEnum::type_uuid() -> 9dec7519-c5f4-12b9-0509-b5b2ef1d521c
AnotherStruct::<Vec<u16>>::type_uuid() -> 933ae311-b69a-f600-7caa-030743cbb5e5