GetSize

Trait GetSize 

Source
pub trait GetSize {
    // Required methods
    fn get_size(&self) -> usize;
    fn max_size(&self) -> Option<usize>;

    // Provided methods
    fn size_is_zero(&self) -> bool { ... }
    fn is_full(&self) -> bool { ... }
}
Expand description

Get the runtime size of some data structure

§Deprecated

Note: in a future version of kwap_common this will be deprecated in favor of clearly delineating “size in bytes” (e.g. RuntimeSize) from “collection of potentially bounded length” (e.g. Len)

§Collections

For collections this just yields the number of elements (Vec::len, tinyvec::ArrayVec::len), and when the collection is over u8s, then get_size represents the number of bytes in the collection.

§Structs and enums

When implemented for items that are not collections, this is expected to yield the runtime size in bytes (not the static Rust core::mem::size_of size)

Required Methods§

Source

fn get_size(&self) -> usize

Get the runtime size (in bytes) of a struct

For collections this is always equivalent to calling an inherent len method.

use kwap_common::GetSize;

assert_eq!(vec![1u8, 2].get_size(), 2)
Source

fn max_size(&self) -> Option<usize>

Get the max size that this data structure can acommodate.

By default, this returns None and can be left unimplemented for dynamic collections.

However, for fixed-size collections this method must be implemented.

use kwap_common::GetSize;

let stack_nums = tinyvec::ArrayVec::<[u8; 2]>::from([0, 1]);
assert_eq!(stack_nums.max_size(), Some(2));

Provided Methods§

Source

fn size_is_zero(&self) -> bool

Check if the runtime size is zero

use kwap_common::GetSize;

assert!(Vec::<u8>::new().size_is_zero())
Source

fn is_full(&self) -> bool

Is there no room left in this collection?

use kwap_common::GetSize;

let array = tinyvec::ArrayVec::<[u8; 2]>::from([1, 2]);

assert!(array.is_full())

Implementations on Foreign Types§

Source§

impl<A: Array> GetSize for ArrayVec<A>

Source§

impl<K, V> GetSize for BTreeMap<K, V>

Source§

impl<K: Eq + Hash, V> GetSize for HashMap<K, V>

Source§

impl<T> GetSize for Vec<T>

Implementors§