1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
// Copyright 2023 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
pub use macros::*;

use {
    std::collections::{BTreeMap, BTreeSet, HashMap, HashSet},
    std::ops::Range,
    std::option::Option,
    std::string::String,
};

/// A TypeFingerprint is able to return a string that represents the layout of a type.
/// It is intended to capture any structure that will affect serialiation via Serde.
pub trait TypeFingerprint {
    fn fingerprint() -> String;
}

// Basic types just return themselves as a string.
macro_rules! impl_fprint_simple {
    ($($($type: ident)::*),*) => {
        $(
            impl TypeFingerprint for $($type)::* {
                fn fingerprint() -> String { stringify!($($type)::*).to_string() }
            }
        )*
    };
}

impl_fprint_simple!(
    bool, i8, i16, i32, i64, i128, isize, u8, u16, u32, u64, u128, usize, f32, f64, str, String
);

// Arrays return their inner type and length (up to a maximum).
macro_rules! impl_fprint_array {
    ($n: literal) => {
        impl<T: TypeFingerprint> TypeFingerprint for [T; $n] {
            fn fingerprint() -> String {
                "[".to_string() + &T::fingerprint() + ";" + stringify!($n) + "]"
            }
        }
    };
}

impl_fprint_array!(0);
impl_fprint_array!(1);
impl_fprint_array!(2);
impl_fprint_array!(3);
impl_fprint_array!(4);
impl_fprint_array!(5);
impl_fprint_array!(6);
impl_fprint_array!(7);
impl_fprint_array!(8);
impl_fprint_array!(9);
impl_fprint_array!(10);
impl_fprint_array!(11);
impl_fprint_array!(12);
impl_fprint_array!(13);
impl_fprint_array!(14);
impl_fprint_array!(15);
impl_fprint_array!(16);
impl_fprint_array!(17);
impl_fprint_array!(18);
impl_fprint_array!(19);
impl_fprint_array!(20);
impl_fprint_array!(21);
impl_fprint_array!(22);
impl_fprint_array!(23);
impl_fprint_array!(24);
impl_fprint_array!(25);
impl_fprint_array!(26);
impl_fprint_array!(27);
impl_fprint_array!(28);
impl_fprint_array!(29);
impl_fprint_array!(30);
impl_fprint_array!(31);
impl_fprint_array!(32);

macro_rules! impl_fprint_one_generic {
    ($($($type: ident)::*),*) => {
        $(
            impl<T: TypeFingerprint> TypeFingerprint for $($type)::*<T> {
                fn fingerprint() -> String {
                    "".to_owned() + stringify!($($type)::*) + "<" + &T::fingerprint() + ">"
                }
            }
        )*
    };
}

impl_fprint_one_generic!(BTreeSet, HashSet, Range, Option, Vec);

macro_rules! impl_fprint_two_generic {
    ($($($type: ident)::*),*) => {
        $(
            impl<A: TypeFingerprint, B: TypeFingerprint> TypeFingerprint for $($type)::*<A,B> {
                fn fingerprint() -> String {
                    "".to_owned() + stringify!($($type)::*) +
                        "<" + &A::fingerprint() + "," + &B::fingerprint() + ">"
                }
            }
        )*
    };
}

impl_fprint_two_generic!(BTreeMap);

macro_rules! impl_fprint_tuple {
    (($($type: ident,)*)) => {
        impl<$($type: TypeFingerprint),*> TypeFingerprint for ($($type,)*) {
            fn fingerprint() -> String {
                "(".to_owned() + $(&$type::fingerprint() + "," +)* ")"
            }
        }
    };
}

impl_fprint_tuple!(());
impl_fprint_tuple!((A,));
impl_fprint_tuple!((A, B,));
impl_fprint_tuple!((A, B, C,));
impl_fprint_tuple!((A, B, C, D,));
impl_fprint_tuple!((A, B, C, D, E,));
impl_fprint_tuple!((A, B, C, D, E, F,));
impl_fprint_tuple!((A, B, C, D, E, F, G,));
impl_fprint_tuple!((A, B, C, D, E, F, G, H,));
impl_fprint_tuple!((A, B, C, D, E, F, G, H, I,));

impl<'a, T: TypeFingerprint + ?Sized> TypeFingerprint for &'a T {
    fn fingerprint() -> String {
        "&".to_owned() + &T::fingerprint()
    }
}

impl<'a, T: TypeFingerprint + ?Sized> TypeFingerprint for &'a mut T {
    fn fingerprint() -> String {
        "&mut".to_owned() + &T::fingerprint()
    }
}

impl<T: TypeFingerprint> TypeFingerprint for [T] {
    fn fingerprint() -> String {
        "[".to_owned() + &T::fingerprint() + "]"
    }
}

impl<T: TypeFingerprint + ?Sized> TypeFingerprint for Box<T> {
    fn fingerprint() -> String {
        "Box<".to_owned() + &T::fingerprint() + ">"
    }
}

impl<K: TypeFingerprint, V: TypeFingerprint, S> TypeFingerprint for HashMap<K, V, S> {
    fn fingerprint() -> String {
        // Serde doesn't store any information about the hash function and all entries are re-hashed
        // when deserialized.
        "HashMap<".to_owned() + &K::fingerprint() + "," + &V::fingerprint() + ">"
    }
}

#[cfg(test)]
mod tests {
    use crate::*;
    struct Foo {}
    impl TypeFingerprint for Foo {
        fn fingerprint() -> String {
            "Foo".to_string()
        }
    }

    #[derive(TypeFingerprint)]
    struct Bar(Foo);

    #[derive(TypeFingerprint)]
    struct Baz {
        foo: Foo,
        bar: Bar,
        bizz: u64, //std::collections::BTreeMap<String, (u64, u32, u16, u8, usize)>,
    }

    #[derive(TypeFingerprint)]
    enum Buzz {
        A(Foo),
        B(Bar),
        C(Baz),
    }

    #[test]
    fn test_simple() {
        assert_eq!(u32::fingerprint(), "u32");
    }

    #[test]
    fn test_array() {
        assert_eq!(<[u32; 3]>::fingerprint(), "[u32;3]");
    }

    #[test]
    fn test_vec() {
        assert_eq!(Vec::<[u32; 3]>::fingerprint(), "std :: vec :: Vec<[u32;3]>");
    }

    #[test]
    fn test_hashmap_and_tuple() {
        assert_eq!(
            std::collections::HashMap::<[u32; 3], (bool, [u64; 8])>::fingerprint(),
            "std :: collections :: HashMap<[u32;3],(bool,[u64;8],)>"
        );
    }

    #[test]
    fn test_hand_implemented() {
        assert_eq!(Foo::fingerprint(), "Foo");
    }

    #[test]
    fn test_struct() {
        assert_eq!(Bar::fingerprint(), "struct Bar {Foo,}");
        assert_eq!(Baz::fingerprint(), "struct Baz {foo:Foo,bar:struct Bar {Foo,},bizz:u64,}");
    }

    #[test]
    fn test_enum() {
        assert_eq!(Buzz::fingerprint(), "enum Buzz { A(Foo,),B(struct Bar {Foo,},),C(struct Baz {foo:Foo,bar:struct Bar {Foo,},bizz:u64,},),}");
    }
}