changelog shortlog graph tags branches changeset files revisions annotate raw help

Mercurial > core / rust/lib/obj/src/object/meter.rs

changeset 698: 96958d3eb5b0
parent: c7165d93a9eb
author: Richard Westhaver <ellis@rwest.io>
date: Fri, 04 Oct 2024 22:04:59 -0400
permissions: -rw-r--r--
description: fixes
1 use std::borrow::Borrow;
2 
3 pub trait Meter<K, V> {
4  /// The type used to store measurements.
5  type Measure: Default + Copy;
6  /// Calculate the size of `key` and `value`.
7  fn measure<Q: ?Sized>(&self, key: &Q, value: &V) -> Self::Measure
8  where
9  K: Borrow<Q>;
10 }
11 
12 /// Size limit based on a simple count of cache items.
13 pub struct Count;
14 
15 impl<K, V> Meter<K, V> for Count {
16  /// Don't store anything, the measurement can be derived from the map.
17  type Measure = ();
18 
19  /// Don't actually count anything either.
20  fn measure<Q: ?Sized>(&self, _: &Q, _: &V)
21  where
22  K: Borrow<Q>,
23  {
24  }
25 }
26 
27 /// A trait to allow the default `Count` measurement to not store an
28 /// extraneous counter.
29 pub trait CountableMeter<K, V>: Meter<K, V> {
30  /// Add `amount` to `current` and return the sum.
31  fn add(&self, current: Self::Measure, amount: Self::Measure) -> Self::Measure;
32  /// Subtract `amount` from `current` and return the difference.
33  fn sub(&self, current: Self::Measure, amount: Self::Measure) -> Self::Measure;
34  /// Return `current` as a `usize` if possible, otherwise return `None`.
35  ///
36  /// If this method returns `None` the cache will use the number of cache
37  /// entries as its size.
38  fn size(&self, current: Self::Measure) -> Option<u64>;
39 }
40 
41 /// `Count` is all no-ops, the number of entries in the map is the size.
42 impl<K, V, T: Meter<K, V>> CountableMeter<K, V> for T
43 where
44  T: CountableMeterWithMeasure<K, V, <T as Meter<K, V>>::Measure>,
45 {
46  fn add(&self, current: Self::Measure, amount: Self::Measure) -> Self::Measure {
47  CountableMeterWithMeasure::meter_add(self, current, amount)
48  }
49  fn sub(&self, current: Self::Measure, amount: Self::Measure) -> Self::Measure {
50  CountableMeterWithMeasure::meter_sub(self, current, amount)
51  }
52  fn size(&self, current: Self::Measure) -> Option<u64> {
53  CountableMeterWithMeasure::meter_size(self, current)
54  }
55 }
56 
57 pub trait CountableMeterWithMeasure<K, V, M> {
58  /// Add `amount` to `current` and return the sum.
59  fn meter_add(&self, current: M, amount: M) -> M;
60  /// Subtract `amount` from `current` and return the difference.
61  fn meter_sub(&self, current: M, amount: M) -> M;
62  /// Return `current` as a `usize` if possible, otherwise return `None`.
63  ///
64  /// If this method returns `None` the cache will use the number of cache
65  /// entries as its size.
66  fn meter_size(&self, current: M) -> Option<u64>;
67 }
68 
69 /// For any other `Meter` with `Measure=usize`, just do the simple math.
70 impl<K, V, T> CountableMeterWithMeasure<K, V, usize> for T
71 where
72  T: Meter<K, V>,
73 {
74  fn meter_add(&self, current: usize, amount: usize) -> usize {
75  current + amount
76  }
77  fn meter_sub(&self, current: usize, amount: usize) -> usize {
78  current - amount
79  }
80  fn meter_size(&self, current: usize) -> Option<u64> {
81  Some(current as u64)
82  }
83 }
84 
85 impl<K, V> CountableMeterWithMeasure<K, V, ()> for Count {
86  fn meter_add(&self, _current: (), _amount: ()) {}
87  fn meter_sub(&self, _current: (), _amount: ()) {}
88  fn meter_size(&self, _current: ()) -> Option<u64> {
89  None
90  }
91 }