Trait lru_mem::ValueSize

source ·
pub trait ValueSize {
    // Required method
    fn value_size(&self) -> usize;

    // Provided methods
    fn value_size_sum_iter<'item>(
        iterator: impl Iterator<Item = &'item Self>
    ) -> usize
       where Self: 'item { ... }
    fn value_size_sum_exact_size_iter<'item>(
        iterator: impl ExactSizeIterator<Item = &'item Self>
    ) -> usize
       where Self: 'item { ... }
}
Expand description

A trait for types whose value size can be determined at runtime. This only refers to the size of the value itself, not allocated data. For Sized types, this is equivalent to mem::size_of, which is provided by a blanket implementation. For unsized types, mem::size_of_val can be used.

Example

use lru_mem::ValueSize;
use std::mem;

// unsized type
struct FlaggedBytes {
    flag: bool,
    bytes: [u8]
}

impl ValueSize for FlaggedBytes {
    fn value_size(&self) -> usize {
        // This is a valid implementation for all unsized types
        mem::size_of_val(self)
    }
}

Required Methods§

source

fn value_size(&self) -> usize

The size of this value in bytes, excluding allocated data.

Example
use lru_mem::ValueSize;
use std::mem;

let boxed = Box::new([0u8; 64]);

assert_eq!(mem::size_of::<*const ()>(), boxed.value_size());

Provided Methods§

source

fn value_size_sum_iter<'item>( iterator: impl Iterator<Item = &'item Self> ) -> usizewhere Self: 'item,

The total sum of the sizes of all values in the given iterator, in bytes. This is default-implemented by computing ValueSize::value_size on every element and summing them. For Sized types, a more potentially efficient implementation using Iterator::count is provided. If you are implementing this trait manually, it is unlikely to be more efficient to provide a manual implementation here.

Example
use lru_mem::ValueSize;

let nums: [i32; 3] = [1, 2, 3];

assert_eq!(8, i32::value_size_sum_iter(nums.iter().filter(|item| **item > 1)));
source

fn value_size_sum_exact_size_iter<'item>( iterator: impl ExactSizeIterator<Item = &'item Self> ) -> usizewhere Self: 'item,

The total sum of the sizes of all values in the given exact-size-iterator, in bytes. This is default-implemented by using ValueSize::value_size_sum_iter. For Sized types, a usually more efficient implementation using ExactSizeIterator::len is provided. If you are implementing this trait manually, it is unlikely to be more efficient to provide a manual implementation here.

Example
use lru_mem::ValueSize;

let nums: [i32; 3] = [1, 2, 3];

assert_eq!(12, i32::value_size_sum_exact_size_iter(nums.iter()));

Implementations on Foreign Types§

source§

impl ValueSize for Path

source§

impl<T> ValueSize for [T]

source§

impl ValueSize for str

source§

impl ValueSize for OsStr

source§

impl ValueSize for CStr

Implementors§

source§

impl<T: Sized> ValueSize for T