contain (0.5.1)

Published 2026-04-19 12:39:16 +01:00 by sam in sam/contain-rs

Installation

[registries.forgejo]
index = "sparse+" # Sparse index
# index = "" # Git

[net]
git-fetch-with-cli = true
cargo add contain@0.5.1 --registry forgejo

About this package

A crate for defining/extending lifetimes

contain-rs

A crate for defining/extending lifetimes.

Examples

Simple Container

A basic fast implementation backed by Vec.

use contain::SimpleContainer;

fn append_thing<'a>(container: &'a SimpleContainer<String>, s: &str) -> &'a str {
    container.put(format!("{}thing", s))
}

fn main() {
    let container = SimpleContainer::new();
    let a = append_thing(&container, "some");
    let b = append_thing(&container, "a ");
    let c = append_thing(&container, "that ");
    assert_eq!(a, "something");
    assert_eq!(b, "a thing");
    assert_eq!(c, "that thing");
    assert_eq!(container.count(), 3);
}

Deduplicating Container

A deduplicating container backed by indexmap::IndexMap. If two equal items are stored, the second is dropped and a reference to the first is returned. Whilst more resource-intensive than SimpleContainer, it can be more memory efficient in scenarios where many items are equal and equivalent since the duplicates will be dropped.

use contain::DeduplicatingContainer;

fn append_thing<'a>(container: &'a DeduplicatingContainer<'_, String>, s: &str) -> &'a str {
    container.put(format!("{}thing", s))
}

fn main() {
    let container = DeduplicatingContainer::new();
    let a = append_thing(&container, "some");
    let b = append_thing(&container, "a ");
    let c = append_thing(&container, "some");
    assert_eq!(a, "something");
    assert_eq!(b, "a thing");
    assert_eq!(c, "something");
    assert_eq!(container.count(), 2);
}

Dependencies

ID Version
indexmap ^2.13
parking_lot ^0.12.1

Keywords

lifetime memory
Details
Cargo
2026-04-19 12:39:16 +01:00
12
Samuel Collins
MIT OR Apache-2.0
9.9 KiB
Assets (1)
Versions (2) View all
0.5.1 2026-04-19
0.5.0 2026-04-03