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
use crate::{Hash, StaticType};
use serde::{Deserialize, Serialize};
use std::fmt;
use std::ops;

/// The type of an entry.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[repr(transparent)]
pub struct Type(Hash);

impl Type {
    /// Construct type from a Rust type identifier.
    pub fn from_type_id(type_id: std::any::TypeId) -> Self {
        Self(Hash::from_type_id(type_id))
    }

    /// Construct a type from a type hash.
    pub const fn from_type_hash(hash: Hash) -> Self {
        Self(hash)
    }
}

impl From<&'static StaticType> for Type {
    fn from(static_type: &'static StaticType) -> Self {
        Self(static_type.hash)
    }
}

impl From<Hash> for Type {
    fn from(hash: Hash) -> Self {
        Self(hash)
    }
}

impl ops::Deref for Type {
    type Target = Hash;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl fmt::Display for Type {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}

#[cfg(test)]
mod tests {
    use super::Type;

    #[test]
    fn test_size() {
        assert_eq! {
            std::mem::size_of::<Type>(),
            8,
        };
    }
}