From 6af588cb2c990333b24b6ab5d4ef3db66cd46f4a Mon Sep 17 00:00:00 2001 From: bluss Date: Fri, 18 Sep 2015 00:52:17 +0200 Subject: [PATCH] ArrayString: Add PartialEq, Eq, Hash, and tests --- src/array_string.rs | 45 +++++++++++++++++++++++++++++++++------------ tests/tests.rs | 21 +++++++++++++++++++++ 2 files changed, 54 insertions(+), 12 deletions(-) diff --git a/src/array_string.rs b/src/array_string.rs index 9552013..d6b6089 100644 --- a/src/array_string.rs +++ b/src/array_string.rs @@ -1,6 +1,6 @@ use std::borrow::Borrow; use std::fmt; -use std::mem; +use std::hash::{Hash, Hasher}; use std::ops::Deref; use std::str; use std::slice; @@ -22,14 +22,6 @@ pub struct ArrayString> { len: A::Index, } -unsafe fn new_array>() -> A { - // Note: Returning an uninitialized value here only works - // if we can be sure the data is never used. The nullable pointer - // inside enum optimization conflicts with this this for example, - // so we need to be extra careful. See `NoDrop` enum. - mem::uninitialized() -} - impl> ArrayString { /// Create a new empty `ArrayString`. /// @@ -46,7 +38,7 @@ impl> ArrayString { pub fn new() -> ArrayString { unsafe { ArrayString { - xs: new_array(), + xs: ::new_array(), len: Index::from(0), } } @@ -150,6 +142,32 @@ impl> Deref for ArrayString { } } +impl> PartialEq for ArrayString { + fn eq(&self, rhs: &Self) -> bool { + **self == **rhs + } +} + +impl> PartialEq for ArrayString { + fn eq(&self, rhs: &str) -> bool { + &**self == rhs + } +} + +impl> PartialEq> for str { + fn eq(&self, rhs: &ArrayString) -> bool { + self == &**rhs + } +} + +impl> Eq for ArrayString { } + +impl> Hash for ArrayString { + fn hash(&self, h: &mut H) { + (**self).hash(h) + } +} + impl> Borrow for ArrayString { fn borrow(&self) -> &str { self } } @@ -158,7 +176,11 @@ impl> AsRef for ArrayString { fn as_ref(&self) -> &str { self } } -impl> fmt::Debug for ArrayString where A::Item: fmt::Debug { +impl> fmt::Debug for ArrayString { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { (**self).fmt(f) } +} + +impl> fmt::Display for ArrayString { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { (**self).fmt(f) } } @@ -174,4 +196,3 @@ impl + Copy> Clone for ArrayString { *self } } - diff --git a/tests/tests.rs b/tests/tests.rs index ac3782f..a06b6e6 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -1,8 +1,11 @@ extern crate arrayvec; use arrayvec::ArrayVec; +use arrayvec::ArrayString; use std::mem; +use std::collections::HashMap; + #[test] fn test_simple() { @@ -260,3 +263,21 @@ fn test_write() { assert_eq!(r, 5); assert_eq!(&v[..], &[1, 2, 3, 9, 9, 9, 9, 9]); } + +#[test] +fn test_string() { + let text = "hello world"; + let mut s = ArrayString::<[_; 16]>::new(); + s.push_str(text).unwrap(); + assert_eq!(&s, text); + assert_eq!(text, &s); + + // Make sure Hash / Eq / Borrow match up so we can use HashMap + let mut map = HashMap::new(); + map.insert(s, 1); + assert_eq!(map[text], 1); + + let mut t = ArrayString::<[_; 2]>::new(); + assert!(t.push_str(text).is_err()); + assert_eq!(&t, ""); +}