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
use crate::type_id::TypeId;
use core::cmp::Ordering;

/// Id of a storage, can be a `TypeId` or `u64`.
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum StorageId {
    /// Identify a Rust type known at compile time
    TypeId(TypeId),
    /// Identify a type only known at runtime
    Custom(u64),
}

impl StorageId {
    /// Returns `T`'s `StorageId`.
    pub fn of<T: 'static>() -> Self {
        TypeId::of::<T>().into()
    }
}

impl From<TypeId> for StorageId {
    fn from(type_id: TypeId) -> Self {
        StorageId::TypeId(type_id)
    }
}

impl From<core::any::TypeId> for StorageId {
    fn from(type_id: core::any::TypeId) -> Self {
        StorageId::TypeId(type_id.into())
    }
}

impl From<u64> for StorageId {
    fn from(int: u64) -> Self {
        StorageId::Custom(int)
    }
}

impl PartialEq<TypeId> for StorageId {
    fn eq(&self, type_id: &TypeId) -> bool {
        if let StorageId::TypeId(self_type_id) = self {
            self_type_id == type_id
        } else {
            false
        }
    }
}

impl PartialOrd<TypeId> for StorageId {
    fn partial_cmp(&self, type_id: &TypeId) -> Option<Ordering> {
        if let StorageId::TypeId(self_type_id) = self {
            self_type_id.partial_cmp(type_id)
        } else {
            Some(Ordering::Less)
        }
    }
}

impl Default for StorageId {
    fn default() -> Self {
        StorageId::Custom(core::u64::MAX)
    }
}

impl core::fmt::Debug for StorageId {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        let mut debug_struct = f.debug_struct("StorageId");

        match self {
            StorageId::TypeId(type_id) => {
                debug_struct.field("TypeId", type_id);
            }
            StorageId::Custom(custom) => {
                debug_struct.field("Custom", custom);
            }
        }

        debug_struct.finish()
    }
}