Trait lru_mem::MemSize

source ·
pub trait MemSize: ValueSize + HeapSize {
    // Required method
    fn mem_size(&self) -> usize;
}
Expand description

A trait for types whose total size in memory can be determined at runtime. This is required for the LruCache to track the size of entries. It has implementations for most common data types and containers.

Note that reference-counting smart pointers deliberately do not implement this trait, as it is not clear whether a pointer will drop the referenced content when it is ejected from the cache.

This trait is blanked-implemented via the HeapSize and ValueSize traits. For Sized types, it suffices to implement HeapSize, otherwise implement both HeapSize and ValueSize. This trait will automatically be implemented.

Required Methods§

source

fn mem_size(&self) -> usize

The total size of this value in bytes. This includes the value itself as well as all owned referenced data (such as the value on the heap of a Box or the elements and reserved memory of a Vec).

This function is blanket-implemented by adding ValueSize::value_size and HeapSize::heap_size for any given value.

Example
use lru_mem::MemSize;
use std::mem;

assert_eq!(8, 1u64.mem_size());
assert_eq!(12 + mem::size_of::<String>(),
    "hello world!".to_owned().mem_size());

Implementors§