Skip to content

Commit 07fa59a

Browse files
committed
feat: impl Hash trait
Now, NonEmptyString can be used in HasMap and HasSet
1 parent 5e4e0e3 commit 07fa59a

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

src/lib.rs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,9 @@ use delegate::delegate;
1717
pub use error::EmptyString;
1818
use std::{fmt::Display, str::FromStr};
1919

20-
21-
2220
/// A simple String wrapper type, similar to NonZeroUsize and friends.
2321
/// Guarantees that the String contained inside is not of length 0.
24-
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
22+
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
2523
#[repr(transparent)]
2624
pub struct NonEmptyString(String);
2725

@@ -177,6 +175,9 @@ impl From<NonEmptyString> for String {
177175

178176
#[cfg(test)]
179177
mod tests {
178+
use std::collections::HashMap;
179+
use std::collections::HashSet;
180+
180181
use super::*;
181182

182183
#[test]
@@ -246,4 +247,19 @@ mod tests {
246247
let non_empty_string = NonEmptyString::new("string".to_string()).unwrap();
247248
let _string = String::from(non_empty_string);
248249
}
250+
251+
#[test]
252+
fn hash_works() {
253+
let mut map = HashMap::new();
254+
map.insert(NonEmptyString::from_str("id.1").unwrap(), 1);
255+
map.insert(NonEmptyString::from_str("id.2").unwrap(), 2);
256+
257+
assert_eq!(map.len(), 2);
258+
259+
let mut set = HashSet::new();
260+
set.insert(NonEmptyString::from_str("1").unwrap());
261+
set.insert(NonEmptyString::from_str("2").unwrap());
262+
263+
assert_eq!(set.len(), 2);
264+
}
249265
}

0 commit comments

Comments
 (0)