# Maplike - Crate with traits for abstract containers and operations on them

**URL:** <https://users.rust-lang.org/t/maplike-crate-with-traits-for-abstract-containers-and-operations-on-them/141106>\
**Category:** announcements\
**Created:** [July 6, 2026, 8:56am UTC](https://users.rust-lang.org/t/maplike-crate-with-traits-for-abstract-containers-and-operations-on-them/141106 "2026-07-06T08:56:37Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mikwielgus](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/mikwielgus/32/53549_2.png) [@mikwielgus](https://users.rust-lang.org/u/mikwielgus)\
**Post date:** [July 6, 2026, 8:56am UTC](https://users.rust-lang.org/t/maplike-crate-with-traits-for-abstract-containers-and-operations-on-them/141106/1 "2026-07-06T08:56:37Z")

</div>

Hello!

I would like to share my crate, [maplike](https://crates.io/crates/maplike). Maplike provides traits for common operations, `.get()`, `.set()`, `.insert()`, `.remove()`, `.push()`, `.pop()`, `.into_iter()`, and so on, over container data structures, such as std's `Vec`, `BTreeMap`, `BTreeSet`, `HashMap`, `HashSet` and for multiple third-party types, e.g. `stable_vec::StableVec`, `thunderdome::Arena`, `tinyvec::ArrayVec`, `tinyvec::TinyVec`.

Link: [GitHub - mikwielgus/maplike: Rust traits for abstract containers and operations over them. Basically, every container is treated as a map. You can have the same code work on Vec, VecDeque, HashMap, BTreeMap, Option, Box, Rc, Arc, and more. · GitHub](https://github.com/mikwielgus/maplike)

I developed this library for myself to make it possible to write code that is generic over different collection-like data types. These types all have considerable similarities between their interfaces, but I couldn't find a suitable library with traits to abstract the shared behavior that I needed, so I rolled my own.

Basically, this is Python's [collections.abc](https://docs.python.org/3/library/collections.abc.html), but in Rust, and with traits not only for different kinds of containers, but also for each operation.

I maintain this library and dogfood it in my other two crates:

- [undoredo](https://github.com/mikwielgus/undoredo): Undo/Redo and non-linear history tree using sparse deltas (diffs), snapshots, or commands on arbitrary data structures.
- [dcel](https://github.com/mikwielgus/dcel): half-edge data structure that is generic over its containers.

Feedback is welcome!

Below are two code examples taken from the readme:

First example. Function that gets the second element of a collection that is generic over `Vec`, array, `BTreeMap`:

```rust
use std::collections::BTreeMap;

use maplike::Get;

// Generic over any collection implementing the `Get` trait.
fn get_second_element<C: Get<usize>>(collection: &C) -> Option<&C::Value> {
    collection.get(&1)
}

// `get_second_element()` works for `Vec`s, arrays, and maps with the very
// same code.
assert_eq!(get_second_element(&vec![10, 20, 30]), Some(&20));
assert_eq!(get_second_element(&[10, 20, 30]), Some(&20));
assert_eq!(get_second_element(&BTreeMap::from([(0, 10), (1, 20)])), Some(&20));use std::collections::BTreeMap; 

```

`maplike` also provides abstract container traits that allow you to abstract over whole collection types, not only operations. Here's code that is generic over `Vec`, `tinyvec::ArrayVec`, `tinyvec::TinyVec` using an abstract collection trait `Veclike`:

```rust
use maplike::{Clear, Push, Veclike};
use tinyvec::{ArrayVec, TinyVec};

// This function is generic over any `Veclike` collection. The `Veclike` bound
// provides `.clear()`, `.push()` and many other methods at once.
fn replace_all<C: Veclike<usize, Value = i32>>(collection: &mut C, values: &[i32]) {
    collection.clear();
    for &value in values {
        collection.push(value);
    }
}

// `replace_all()` now works for any `Veclike` collection.

// Works on `Vec`,
let mut vec = Vec::new();
replace_all(&mut vec, &[1, 2, 3]);
assert_eq!(vec, [1, 2, 3]);
replace_all(&mut vec, &[4, 5, 6]);
assert_eq!(vec, [4, 5, 6]);

// Works on `tinyvec::ArrayVec`.
let mut array_vec: ArrayVec<[i32; 8]> = ArrayVec::new();
replace_all(&mut array_vec, &[7, 8, 9]);
assert_eq!(array_vec.as_slice(), [7, 8, 9]);

// Works on `tinyvec::TinyVec`.
let mut tiny_vec: TinyVec<[i32; 8]> = TinyVec::new();
replace_all(&mut tiny_vec, &[10, 11, 12]);
assert_eq!(tiny_vec.as_slice(), [10, 11, 12]);use maplike::{Clear, Push, Veclike};

```

---

<div class="post-metadata">

**Author:** ![MusicalNinjaDad](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/musicalninjadad/32/41229_2.png) [@MusicalNinjaDad](https://users.rust-lang.org/u/MusicalNinjaDad)\
**Post date:** [July 6, 2026, 11:12am UTC](https://users.rust-lang.org/t/maplike-crate-with-traits-for-abstract-containers-and-operations-on-them/141106/2 "2026-07-06T11:12:45Z")

</div>

That looks pretty useful.  
What would possbily make it even better would be too add an `Entry` as well. That's a really powerful function on `HashMap` etc. and could possibly even be applied to Vec with `K: usize`(??) or by including it in `Get` and adding an Entry trait for the return type.

---

<div class="post-metadata">

**Author:** ![mikwielgus](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/mikwielgus/32/53549_2.png) [@mikwielgus](https://users.rust-lang.org/u/mikwielgus)\
**Post date:** [July 6, 2026, 8:55pm UTC](https://users.rust-lang.org/t/maplike-crate-with-traits-for-abstract-containers-and-operations-on-them/141106/3 "2026-07-06T20:55:56Z")

</div>

Thanks for reminding me of this, traits for the entry interface are definitely something I need to add.

---

<div class="post-metadata">

**Author:** ![mikwielgus](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/mikwielgus/32/53549_2.png) [@mikwielgus](https://users.rust-lang.org/u/mikwielgus)\
**Post date:** [July 8, 2026, 8:48pm UTC](https://users.rust-lang.org/t/maplike-crate-with-traits-for-abstract-containers-and-operations-on-them/141106/4 "2026-07-08T20:48:16Z")

</div>

> and could possibly even be applied to Vec with `K: usize`(??)

I don't think creating an Entry interface for `Vec` itself (as opposed to `BTreeMap`, `HashMap`, `IndexMap`) is a good idea, because if you hit an index higher than `.len()`, then to be able to insert an element it will be necessary to create a padding of one or more elements (presumably all `Default::default()`) to fill what is between it and the `Vec`'s back. Having an insert operation insert more than what the caller provided is not what the caller is likely to expect and would be considerably different from what map-like structures do on insert.

But I can imagine `StableVec` having an Entry interface. In `maplike`, I already have `.insert()` implemented for it by first extending it using its `.reserve_for()`, so perhaps an issue for this could be opened on `stable_vec`'s issue tracker.

---

<div class="post-metadata">

**Author:** ![mikwielgus](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/mikwielgus/32/53549_2.png) [@mikwielgus](https://users.rust-lang.org/u/mikwielgus)\
**Post date:** [July 17, 2026, 12:31pm UTC](https://users.rust-lang.org/t/maplike-crate-with-traits-for-abstract-containers-and-operations-on-them/141106/5 "2026-07-17T12:31:13Z")

</div>

I have added a [generic Entry API](https://docs.rs/maplike/latest/maplike/entry/trait.Entry.html) for maplike.

I have [requested](https://github.com/LukasKalbertodt/stable-vec/issues/50) Entry API on stable-vec's issue tracker.

---

<div class="post-metadata">

**Author:** ![system](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/system/32/51372_2.png) [@system](https://users.rust-lang.org/u/system)\
**Post date:** [October 15, 2026, 12:31pm UTC](https://users.rust-lang.org/t/maplike-crate-with-traits-for-abstract-containers-and-operations-on-them/141106/6 "2026-10-15T12:31:21Z")

</div>

Closed on October 15, 2026, 12:31pm UTC

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.
