Skip to content

Commit

Permalink
#17 eq.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Apr 19, 2023
1 parent a9366d2 commit 702dd44
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 22 deletions.
56 changes: 56 additions & 0 deletions src/eq.rs
@@ -0,0 +1,56 @@
// Copyright (c) 2023 Yegor Bugayenko
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

use crate::Map;

impl<K: Copy + PartialEq, V: Copy + PartialEq, const N: usize> PartialEq for Map<K, V, N> {
/// ```
/// let mut m1: micromap::Map<u8, i32, 10> = micromap::Map::new();
/// let mut m2: micromap::Map<u8, i32, 10> = micromap::Map::new();
/// m1.insert(1, 42);
/// m2.insert(1, 42);
/// assert_eq!(m1, m2);
///
/// // two maps with different order of key-value pairs are still equal:
/// m1.insert(2, 1);
/// m1.insert(3, 16);
/// m2.insert(3, 16);
/// m2.insert(2, 1);
/// assert_eq!(m1, m2);
/// ```
fn eq(&self, other: &Self) -> bool {
return self.len() == other.len() && self.iter().all(|(k, v)| other.get(k) == Some(v));
}
}

impl<K: Copy + Eq, V: Copy + Eq, const N: usize> Eq for Map<K, V, N> {}

#[cfg(test)]
use anyhow::Result;

#[test]
fn compares_two_maps() -> Result<()> {
let mut m1: Map<&str, i32, 10> = Map::new();
m1.insert("first", 42);
let mut m2: Map<&str, i32, 10> = Map::new();
m2.insert("first", 42);
assert!(m1.eq(&m2));
Ok(())
}
1 change: 1 addition & 0 deletions src/lib.rs
Expand Up @@ -37,6 +37,7 @@

mod ctors;
mod debug;
mod eq;
mod index;
mod iterators;
mod map;
Expand Down
22 changes: 0 additions & 22 deletions src/map.rs
Expand Up @@ -167,28 +167,6 @@ impl<K: Copy + PartialEq, V: Clone + Copy, const N: usize> Map<K, V, N> {
}
}

impl<K: Copy + PartialEq, V: Copy + PartialEq, const N: usize> PartialEq for Map<K, V, N> {
/// ```
/// let mut m1: micromap::Map<u8, i32, 10> = micromap::Map::new();
/// let mut m2: micromap::Map<u8, i32, 10> = micromap::Map::new();
/// m1.insert(1, 42);
/// m2.insert(1, 42);
/// assert_eq!(m1, m2);
///
/// // two maps with different order of key-value pairs are still equal:
/// m1.insert(2, 1);
/// m1.insert(3, 16);
/// m2.insert(3, 16);
/// m2.insert(2, 1);
/// assert_eq!(m1, m2);
/// ```
fn eq(&self, other: &Self) -> bool {
return self.len() == other.len() && self.iter().all(|(k, v)| other.get(k) == Some(v));
}
}

impl<K: Copy + Eq, V: Copy + Eq, const N: usize> Eq for Map<K, V, N> {}

#[cfg(test)]
use anyhow::Result;

Expand Down

0 comments on commit 702dd44

Please sign in to comment.