Skip to content

Commit

Permalink
#5 Copy trait
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Apr 16, 2023
1 parent 8d0a274 commit 3c4d1c3
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 18 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ for very small maps. It is also faster than
performance. When the map is larger than 50, it is better to use standard
[`HashMap`](https://doc.rust-lang.org/std/collections/struct.HashMap.html).

The only important restriction is that both key and value must implement the `Copy` trait.

**WELCOME**:
Not all functions that a user expects to have in a map are implemented.
I will appreciate if you contribute by implementing these missing functions.
Expand Down
6 changes: 3 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ mod pair;
mod serialization;

/// A pair in the Map.
#[derive(Clone, Default)]
#[derive(Clone, Default, Copy)]
enum Pair<K, V> {
Present((K, V)),
#[default]
Expand All @@ -55,8 +55,8 @@ enum Pair<K, V> {
/// because it doesn't use heap. When a `Map` is being created, it allocates the necessary
/// space on stack. That's why the maximum size of the map must be provided in
/// compile time.
#[derive(Clone)]
pub struct Map<K: Copy + PartialEq, V: Clone, const N: usize> {
#[derive(Clone, Copy)]
pub struct Map<K: Copy + PartialEq, V: Clone + Copy, const N: usize> {
pairs: [Pair<K, V>; N],
}

Expand Down
18 changes: 9 additions & 9 deletions src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ impl<'a, K: Clone, V: Clone, const N: usize> Iterator for IntoIter<'a, K, V, N>
}
}

impl<'a, K: Copy + PartialEq, V: Clone, const N: usize> IntoIterator for &'a Map<K, V, N> {
impl<'a, K: Copy + PartialEq, V: Clone + Copy, const N: usize> IntoIterator for &'a Map<K, V, N> {
type Item = (K, V);
type IntoIter = IntoIter<'a, K, V, N>;

Expand All @@ -69,19 +69,19 @@ impl<'a, K: Copy + PartialEq, V: Clone, const N: usize> IntoIterator for &'a Map
}
}

impl<K: Copy + PartialEq, V: Clone, const N: usize> Default for Map<K, V, N> {
impl<K: Copy + PartialEq, V: Clone + Copy, const N: usize> Default for Map<K, V, N> {
fn default() -> Self {
Self::new()
}
}

impl<K: Copy + PartialEq, V: Clone, const N: usize> Map<K, V, N> {
impl<K: Copy + PartialEq, V: Clone + Copy, const N: usize> Map<K, V, N> {
/// Make it.
#[inline]
#[must_use]
pub fn new() -> Self {
Self {
pairs: [(); N].map(|_| Pair::<K, V>::default()),
pairs: [Pair::<K, V>::default(); N],
}
}

Expand Down Expand Up @@ -321,22 +321,22 @@ fn removes_simple_pair() -> Result<()> {
}

#[cfg(test)]
#[derive(Clone)]
#[derive(Clone, Copy)]
struct Foo {
v: Vec<u32>,
v: [u32; 3],
}

#[test]
fn insert_struct() -> Result<()> {
let mut m: Map<u8, Foo, 8> = Map::new();
let foo = Foo { v: vec![1, 2, 100] };
let foo = Foo { v: [1, 2, 100] };
m.insert(1, foo);
assert_eq!(100, m.into_iter().next().unwrap().1.v[2]);
Ok(())
}

#[cfg(test)]
#[derive(Clone)]
#[derive(Clone, Copy)]
struct Composite {
r: Map<u8, u8, 1>,
}
Expand All @@ -350,7 +350,7 @@ fn insert_composite() -> Result<()> {
Ok(())
}

#[derive(Clone)]
#[derive(Clone, Copy)]
struct Bar {}

#[test]
Expand Down
18 changes: 13 additions & 5 deletions src/serialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::fmt::Formatter;
use std::marker::PhantomData;

impl<K: Copy + PartialEq + Serialize, V: Clone + Serialize, const N: usize> Serialize
impl<K: Copy + PartialEq + Serialize, V: Clone + Copy + Serialize, const N: usize> Serialize
for Map<K, V, N>
{
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
Expand All @@ -42,8 +42,12 @@ impl<K: Copy + PartialEq + Serialize, V: Clone + Serialize, const N: usize> Seri

struct Vi<K, V, const N: usize>(PhantomData<K>, PhantomData<V>);

impl<'de, K: Copy + PartialEq + Deserialize<'de>, V: Clone + Deserialize<'de>, const N: usize>
Visitor<'de> for Vi<K, V, N>
impl<
'de,
K: Copy + PartialEq + Deserialize<'de>,
V: Clone + Copy + Deserialize<'de>,
const N: usize,
> Visitor<'de> for Vi<K, V, N>
{
type Value = Map<K, V, N>;

Expand All @@ -63,8 +67,12 @@ impl<'de, K: Copy + PartialEq + Deserialize<'de>, V: Clone + Deserialize<'de>, c
}
}

impl<'de, K: Copy + PartialEq + Deserialize<'de>, V: Clone + Deserialize<'de>, const N: usize>
Deserialize<'de> for Map<K, V, N>
impl<
'de,
K: Copy + PartialEq + Deserialize<'de>,
V: Clone + Copy + Deserialize<'de>,
const N: usize,
> Deserialize<'de> for Map<K, V, N>
{
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
Expand Down
2 changes: 1 addition & 1 deletion tests/vs_std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ pub fn with_micromap(total: usize) -> i64 {

#[test]
pub fn main() {
let total = 100000;
let total = 1000000;
let start1 = Instant::now();
let s1 = with_std(total);
let e1 = start1.elapsed();
Expand Down

0 comments on commit 3c4d1c3

Please sign in to comment.