3 unstable releases

0.4.0 Sep 9, 2022
0.3.1 Sep 9, 2022
0.2.1 Sep 8, 2022
0.1.0 Sep 2, 2022

#458 in Memory management

Download history 1/week @ 2024-07-27 80/week @ 2024-08-10 415/week @ 2024-09-07 1/week @ 2024-09-14 12/week @ 2024-10-26 100/week @ 2024-11-02

112 downloads per month

MIT/Apache

19KB
374 lines

contain-rs

A crate for defining/extending lifetimes.

Examples

Simple Container

A basic fast implementation of Container backed by Vec.

use contain::{Container, SimpleContainer};

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

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.len(), 3)

Deduplicating Container

A deduplicating Container backed by a std::collections::HashSet. 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::{Container, DeduplicatingContainer};

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

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.len(), 2);

Dependencies

~0.4–5MB
~11K SLoC