tor_memquota_cost/memory_cost.rs
1//! `HasMemoryCost` and typed memory cost tracking
2
3/// Types whose memory usage is known (and stable)
4///
5/// ### Important guarantees
6///
7/// Implementors of this trait must uphold the guarantees in the API of
8/// [`memory_cost`](HasMemoryCost::memory_cost).
9///
10/// If these guarantees are violated, memory tracking may go wrong,
11/// with seriously bad implications for the whole program,
12/// including possible complete denial of service.
13///
14/// (Nevertheless, memory safety will not be compromised,
15/// so trait this is not `unsafe`.)
16pub trait HasMemoryCost {
17 /// Returns the memory cost of `self`, in bytes
18 ///
19 /// ### Return value must be stable
20 ///
21 /// It is vital that the return value does not change, for any particular `self`,
22 /// unless `self` is mutated through `&mut self` or similar.
23 /// Otherwise, memory accounting may go awry.
24 ///
25 /// If `self` has interior mutability. the changing internal state
26 /// must not change the memory cost.
27 ///
28 /// ### Panics - forbidden
29 ///
30 /// This method must not panic.
31 /// Otherwise, memory accounting may go awry.
32 fn memory_cost(&self, _: crate::EnabledToken) -> usize;
33}