#unique #entries #vec #push #serde #eq

uniquevec

A Vec-like datastructure which only contains unique entries. It is no_std and has optional serde support.

1 unstable release

0.1.0 Nov 4, 2024

#769 in Data structures

Download history 78/week @ 2024-10-29 187/week @ 2024-11-05 287/week @ 2024-11-12

552 downloads per month
Used in 2 crates (via cellular_raza-core)

MIT/Apache

11KB
130 lines

uniquevec


A Vec-like datastructure which only contains unique entries. It is no_std and makes no use of unsafe code.

License MIT License Apache Test Status Crate Docs


lib.rs:

This crate offers a [Vec]-like datastructure which only contains unique entries. It is no_std by default but necessarily requires an allocator. To access elements inside the vector, we implement the Deref trait such that it can be used like a regular [Vec]. We provide distinct types for [PartialEq] and [Eq] traits in order to support a wider variety of use-cases. To modify entries or create new instances, we implement methods which are listed below.

// Create a new empty UniqueVec
use uniquevec::UniqueVec;
let mut uvec = UniqueVec::new();

// Fill it with contents
uvec.push("cellular");
uvec.push("_");
uvec.push("raza");

// The next entry is already contained so it will not be added again.
// We can also check the returned value
let r = uvec.push("cellular");
assert_eq!(r, Some("cellular"));

// Otherwise we can use it similarly to a Vec
assert_eq!(uvec[0], "cellular");
assert_eq!(uvec[1], "_");
assert_eq!(uvec[2], "raza");
assert_eq!(uvec.len(), 3);

for (n, entry) in uvec.into_iter().enumerate() {
    println!("{n}th entry: {entry}");
}

Create and Modify

Method Description
UniqueVec::new() Creates a new empty [UniqueVec].
UniqueVec::from_iter(iterator) Creates a new [UniqueVec] from an iterator.
UniqueVec::push(item) Pushes a new entry to the back or returns it if already present.
UniqueVec::clear() Clears all entries.
UniqueVec::pop() Removes and returns the last entry.
UniqueVec::extend_from_iter(iterator) Extends elements by the given iterator. Returns duplicates in order.

Implemented Traits

Trait Implemented Comment
Deref for [Vec]
DerefMut See the "Create and Modify" table above.
[Extend]
[From] for [Vec]
[IntoIterator]

[PartialEq] Warning

Since the [UniqueVec] struct only requires the [PartialEq] trait, some unexpected behaviour might occurr when using it with types such as [f32] or [f64] which do not implement [Eq].

let mut unique_vec = UniqueVec::new();

// Insert two times NAN values
unique_vec.push(1f64);
unique_vec.push(f64::NAN);
unique_vec.push(f64::NAN);
assert_eq!(unique_vec[0], 1f64);
assert!(unique_vec[1].is_nan());
assert!(unique_vec[2].is_nan());
assert_eq!(unique_vec.len(), 3);

For this particular reason, we provide the [UniqueVecEq] struct which can only be used when the entry type implements the [Eq] trait.

// This will compile
let mut unique_vec: UniqueVecEq<_> = UniqueVec::new().into();
unique_vec.push(1usize);
// This will not compile
let mut unique_vec: UniqueVecEq<_> = UniqueVec::new().into();
unique_vec.push(1f64);

Features

  • The serde feature offers serialization support.

Dependencies

~165KB