Skip to content

Commit d148529

Browse files
committed
Preemptively avoid an upcoming coherence warning.
Patch as suggested by @arielb1: rust-lang/rust#46192 (comment)
1 parent b3b97fb commit d148529

File tree

1 file changed

+36
-8
lines changed

1 file changed

+36
-8
lines changed

lru-disk-cache/src/lru_cache.rs

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -100,21 +100,49 @@ pub trait CountableMeter<K, V>: Meter<K, V> {
100100
}
101101

102102
/// `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>;
107127
}
108128

109129
/// 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 {
112134
current + amount
113135
}
114-
fn sub(&self, current: usize, amount: usize) -> usize {
136+
fn meter_sub(&self, current: usize, amount: usize) -> usize {
115137
current - amount
116138
}
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 }
118146
}
119147

120148
#[cfg(feature = "heapsize")]

0 commit comments

Comments
 (0)