#map-set #array #vec #vec-map #capped

no-std capped_collections

Collections with compile-time set capacities

2 unstable releases

Uses new Rust 2024

0.2.0 Mar 25, 2026
0.1.0 Mar 12, 2025

#451 in Data structures

MIT/Apache

49KB
1K SLoC

Capped Collections

Crates.io License Downloads Docs Twitch Status

X | Twitch | Youtube | Mastodon | GitHub | GitHub Sponsors

Collections with compile-time set capacities.

Examples:

CappedVec


    use capped_collections::CappedVec;

    //Initialising and pushing values

    let mut capped_vec = CappedVec::<i32, 5>::new();

    capped_vec.push(1);

    capped_vec.push(2);

    capped_vec.push(3);

    capped_vec.push(4);

    assert_eq!(capped_vec.len(), 4);


    use capped_collections::CappedVec;

    //Pushing then poping

    let mut capped_vec = CappedVec::<i32, 5>::new();
    
    capped_vec.push(1);

    capped_vec.push(2);

    capped_vec.pop();

    capped_vec.pop();

    assert_eq!(capped_vec.len(), 0);


    use capped_collections::CappedVec;

    use inc_dec::IncDecSelf;

    //Pushing then iterating the contents.

    let mut capped_vec = CappedVec::<i32, 5>::new();

    capped_vec.push(1);

    capped_vec.push(2);

    capped_vec.push(3);

    capped_vec.push(4);

    let mut i = 1;

    for item in capped_vec.iter()
    {

        assert_eq!(*item, i);

        i.pp();

    }


    use capped_collections::CappedVec;

    //Reseting

    let mut capped_vec = CappedVec::<i32, 5>::new();

    capped_vec.push(1);

    capped_vec.push(2);

    capped_vec.push(3);

    capped_vec.push(4);

    assert_eq!(capped_vec.len(), 4);

    //Reset the CappedVec without iterating and removing each element (i.e. clearing).

    capped_vec.reset();

    assert_eq!(capped_vec.len(), 0);


What Are Capped Collections?

Capped collection objects are structured around the core array type making them suitable for situations where you only need to collect a few items at a time.


Stability Matrix

Object Status
CappedMap Not Tested
CappedQueue Not Tested
CappedSet Not Tested
CappedVec Mostly Tested
CappedVecDeque Broken

Features

Feature Description
no_std Include the no_std crate-level attribute.
serde Enable serde support (incomplete).

Todo:

  • Add more documentation
  • Add more code examples
  • Add more tests
  • Clean-up the code

Maybe:

  • Add a capped linked list with an accompanying Rc type.
  • Add a capped priority queue type.
  • Add ordered map and set types.
  • Add collection construction macros
  • Add a capped string type.

Code Style

This project uses a coding style the emphasises the use of white space over keeping the line and column counts as low as possible.

So this:


fn bar()
{
}

fn foo()
{

    bar();

}

Not this:


fn bar(){}

fn foo()
{
    bar();
}


License

Licensed under either of:

at your discretion


Contributing

Please clone the repository and create an issue explaining what feature or features you'd like to add or bug or bugs you'd like to fix and perhaps how you intend to implement these additions or fixes. Try to include details though it doesn't need to be exhaustive and we'll take it from there (dependant on availability).


Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~140–570KB
~14K SLoC