Mercurial > core / rust/lib/obj/src/object/meter.rs
changeset 17: |
c7165d93a9eb |
author: |
ellis <ellis@rwest.io> |
date: |
Sun, 22 Oct 2023 23:03:15 -0400 |
permissions: |
-rw-r--r-- |
description: |
add obj and net src |
1 use std::borrow::Borrow; 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 12 /// Size limit based on a simple count of cache items. 15 impl<K, V> Meter<K, V> for Count { 16 /// Don't store anything, the measurement can be derived from the map. 19 /// Don't actually count anything either. 20 fn measure<Q: ?Sized>(&self, _: &Q, _: &V) 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`. 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>; 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 44 T: CountableMeterWithMeasure<K, V, <T as Meter<K, V>>::Measure>, 46 fn add(&self, current: Self::Measure, amount: Self::Measure) -> Self::Measure { 47 CountableMeterWithMeasure::meter_add(self, current, amount) 49 fn sub(&self, current: Self::Measure, amount: Self::Measure) -> Self::Measure { 50 CountableMeterWithMeasure::meter_sub(self, current, amount) 52 fn size(&self, current: Self::Measure) -> Option<u64> { 53 CountableMeterWithMeasure::meter_size(self, current) 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`. 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>; 69 /// For any other `Meter` with `Measure=usize`, just do the simple math. 70 impl<K, V, T> CountableMeterWithMeasure<K, V, usize> for T 74 fn meter_add(&self, current: usize, amount: usize) -> usize { 77 fn meter_sub(&self, current: usize, amount: usize) -> usize { 80 fn meter_size(&self, current: usize) -> Option<u64> { 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> {