Skip to content

Add as_slice functions to map and set with corresponding tests #42

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "linear-map"
version = "1.2.0"
version = "1.2.1"
license = "MIT/Apache-2.0"
description = "A map implemented by searching linearly in a vector."
authors = [
Expand Down
7 changes: 7 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,13 @@ impl<K: Eq, V> LinearMap<K, V> {
}
}

/// Returns a a slice viewing the map's keys and references in arbitrary order.
///
/// The item type is `(K, V)`.
pub fn as_slice(&self) -> &[(K, V)] {
&self.storage
}

/// Returns an iterator yielding references to the map's keys in arbitrary order.
///
/// The iterator's item type is `&K`.
Expand Down
7 changes: 7 additions & 0 deletions src/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,13 @@ where
{
self.map.remove(value).is_some()
}

/// Returns a a slice viewing the sets values in arbitrary order.
///
/// The item type is `(T, ())`.
pub fn as_slice(&self) -> &[(T, ())] {
&self.map.storage
}
}

impl<T> PartialEq for LinearSet<T>
Expand Down
15 changes: 15 additions & 0 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,21 @@ fn test_macro() {
};
}

#[test]
fn test_as_slice() {
use linear_map::set::LinearSet;
let names = linear_map! {
1 => "one",
2 => "two",
};
let slice = names.as_slice();
assert_eq!(slice, &[(1, "one"), (2, "two")]);
let names: LinearSet<&'static str> = names.into_iter().map(|x| x.1).collect::<LinearSet<_>>();
// LinearSet have (T, ()) as items as an implementation detail.
let slice = names.as_slice();
assert_eq!(slice, &[("one", ()), ("two", ())]);
}

#[test]
fn test_retain() {
let mut map: LinearMap<isize, isize> = (0..100).map(|x| (x, x * 10)).collect();
Expand Down