@@ -15,46 +15,56 @@ use crate::system::{
15
15
ExclusiveSystemParamFunction , IsExclusiveFunctionSystem , IsFunctionSystem , SystemParamFunction ,
16
16
} ;
17
17
18
- type Interner = RwLock < HashMap < UniqueValueHash , & ' static str > > ;
18
+ type Interner = RwLock < HashMap < UniqueValue , & ' static str > > ;
19
19
static INTERNER : OnceCell < Interner > = OnceCell :: new ( ) ;
20
20
21
21
/// The [`TypeId`](std::any::TypeId) of a value and its [`DynHash`] output when hashed with the [`DefaultHasher`].
22
22
///
23
23
/// This should be different for each value.
24
24
#[ derive( Clone , Copy , Eq , PartialEq , Hash ) ]
25
- struct UniqueValueHash ( TypeId , u64 ) ;
25
+ struct UniqueValue ( TypeId , u64 ) ;
26
+
27
+ /// Returns a reference to the interned `Debug` string of `value`.
28
+ ///
29
+ /// If the string has not been interned, it will be allocated in a [`Box`] and leaked, but
30
+ /// subsequent calls with the same `value` will return the same reference.
31
+ fn intern_debug < T > ( value : & T ) -> & ' static str
32
+ where
33
+ T : Debug + DynHash + ?Sized ,
34
+ {
35
+ let mut hasher = DefaultHasher :: new ( ) ;
36
+ value. dyn_hash ( & mut hasher) ;
37
+ let hash = hasher. finish ( ) ;
38
+
39
+ let key = UniqueValue ( TypeId :: of :: < T > ( ) , hash) ;
40
+ let mut map = INTERNER . get_or_init ( default) . write ( ) . unwrap ( ) ;
41
+ let str = * map. entry ( key) . or_insert_with ( || {
42
+ let string = format ! ( "{:?}" , value) ;
43
+ let str: & ' static str = Box :: leak ( string. into_boxed_str ( ) ) ;
44
+ str
45
+ } ) ;
46
+
47
+ str
48
+ }
26
49
27
50
/// Identifies a [`Schedule`](super::Schedule).
28
51
pub trait ScheduleLabel : DynHash + Debug + Send + Sync + ' static { }
29
52
30
53
/// A lightweight and printable identifier for a [`Schedule`](super::Schedule).
31
54
#[ derive( Clone , Copy , Eq , PartialEq , Hash ) ]
32
- pub struct ScheduleId ( UniqueValueHash , & ' static str ) ;
55
+ pub struct ScheduleId ( & ' static str ) ;
33
56
34
57
impl ScheduleId {
35
58
/// Returns the [`ScheduleId`] of the schedule.
36
59
pub fn of < S : ScheduleLabel + ?Sized > ( label : & S ) -> ScheduleId {
37
- let mut hasher = DefaultHasher :: new ( ) ;
38
- label. dyn_hash ( & mut hasher) ;
39
- let hash = hasher. finish ( ) ;
40
-
41
- let type_id = TypeId :: of :: < S > ( ) ;
42
-
43
- let key = UniqueValueHash ( type_id, hash) ;
44
- let mut map = INTERNER . get_or_init ( default) . write ( ) . unwrap ( ) ;
45
- let str = * map. entry ( key) . or_insert_with ( || {
46
- let string = format ! ( "{:?}" , label) ;
47
- let str: & ' static str = Box :: leak ( string. into_boxed_str ( ) ) ;
48
- str
49
- } ) ;
50
-
51
- ScheduleId ( key, str)
60
+ let str = intern_debug ( label) ;
61
+ ScheduleId ( str)
52
62
}
53
63
}
54
64
55
65
impl Debug for ScheduleId {
56
66
fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
57
- f . pad ( self . 1 )
67
+ self . 0 . fmt ( f )
58
68
}
59
69
}
60
70
@@ -89,32 +99,19 @@ pub trait SystemSet: DynHash + Debug + Send + Sync + 'static {
89
99
90
100
/// A lightweight and printable identifier for a [`SystemSet`].
91
101
#[ derive( Clone , Copy , Eq , PartialEq , Hash ) ]
92
- pub struct SystemSetId ( UniqueValueHash , & ' static str ) ;
102
+ pub struct SystemSetId ( & ' static str ) ;
93
103
94
104
impl SystemSetId {
95
105
/// Returns the [`SystemSetId`] of the set.
96
106
pub fn of < S : SystemSet + ?Sized > ( set : & S ) -> SystemSetId {
97
- let mut hasher = DefaultHasher :: new ( ) ;
98
- set. dyn_hash ( & mut hasher) ;
99
- let hash = hasher. finish ( ) ;
100
-
101
- let type_id = TypeId :: of :: < S > ( ) ;
102
-
103
- let key = UniqueValueHash ( type_id, hash) ;
104
- let mut map = INTERNER . get_or_init ( default) . write ( ) . unwrap ( ) ;
105
- let str = * map. entry ( key) . or_insert_with ( || {
106
- let string = format ! ( "{:?}" , set) ;
107
- let str: & ' static str = Box :: leak ( string. into_boxed_str ( ) ) ;
108
- str
109
- } ) ;
110
-
111
- SystemSetId ( key, str)
107
+ let str = intern_debug ( set) ;
108
+ SystemSetId ( str)
112
109
}
113
110
}
114
111
115
112
impl Debug for SystemSetId {
116
113
fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
117
- f . pad ( self . 1 )
114
+ self . 0 . fmt ( f )
118
115
}
119
116
}
120
117
0 commit comments