@@ -100,21 +100,49 @@ pub trait CountableMeter<K, V>: Meter<K, V> {
100
100
}
101
101
102
102
/// `Count` is all no-ops, the number of entries in the map is the size.
103
- impl < K , V > CountableMeter < K , V > for Count {
104
- fn add ( & self , _current : ( ) , _amount : ( ) ) -> ( ) { }
105
- fn sub ( & self , _current : ( ) , _amount : ( ) ) -> ( ) { }
106
- fn size ( & self , _current : ( ) ) -> Option < usize > { None }
103
+ impl < K , V , T : Meter < K , V > > CountableMeter < K , V > for T
104
+ where T : CountableMeterWithMeasure < K , V , <T as Meter < K , V > >:: Measure >
105
+ {
106
+ fn add ( & self , current : Self :: Measure , amount : Self :: Measure ) -> Self :: Measure {
107
+ CountableMeterWithMeasure :: meter_add ( self , current, amount)
108
+ }
109
+ fn sub ( & self , current : Self :: Measure , amount : Self :: Measure ) -> Self :: Measure {
110
+ CountableMeterWithMeasure :: meter_sub ( self , current, amount)
111
+ }
112
+ fn size ( & self , current : Self :: Measure ) -> Option < usize > {
113
+ CountableMeterWithMeasure :: meter_size ( self , current)
114
+ }
115
+ }
116
+
117
+ pub trait CountableMeterWithMeasure < K , V , M > {
118
+ /// Add `amount` to `current` and return the sum.
119
+ fn meter_add ( & self , current : M , amount : M ) -> M ;
120
+ /// Subtract `amount` from `current` and return the difference.
121
+ fn meter_sub ( & self , current : M , amount : M ) -> M ;
122
+ /// Return `current` as a `usize` if possible, otherwise return `None`.
123
+ ///
124
+ /// If this method returns `None` the cache will use the number of cache entries as
125
+ /// its size.
126
+ fn meter_size ( & self , current : M ) -> Option < usize > ;
107
127
}
108
128
109
129
/// For any other `Meter` with `Measure=usize`, just do the simple math.
110
- impl < K , V , T : Meter < K , V , Measure =usize > > CountableMeter < K , V > for T {
111
- fn add ( & self , current : usize , amount : usize ) -> usize {
130
+ impl < K , V , T > CountableMeterWithMeasure < K , V , usize > for T
131
+ where T : Meter < K , V >
132
+ {
133
+ fn meter_add ( & self , current : usize , amount : usize ) -> usize {
112
134
current + amount
113
135
}
114
- fn sub ( & self , current : usize , amount : usize ) -> usize {
136
+ fn meter_sub ( & self , current : usize , amount : usize ) -> usize {
115
137
current - amount
116
138
}
117
- fn size ( & self , current : usize ) -> Option < usize > { Some ( current) }
139
+ fn meter_size ( & self , current : usize ) -> Option < usize > { Some ( current) }
140
+ }
141
+
142
+ impl < K , V > CountableMeterWithMeasure < K , V , ( ) > for Count {
143
+ fn meter_add ( & self , _current : ( ) , _amount : ( ) ) { }
144
+ fn meter_sub ( & self , _current : ( ) , _amount : ( ) ) { }
145
+ fn meter_size ( & self , _current : ( ) ) -> Option < usize > { None }
118
146
}
119
147
120
148
#[ cfg( feature = "heapsize" ) ]
0 commit comments