ArrayString: Add PartialEq, Eq, Hash, and tests

This commit is contained in:
bluss
2015-09-18 00:52:17 +02:00
parent fcd7b28124
commit 6af588cb2c
2 changed files with 54 additions and 12 deletions
+33 -12
View File
@@ -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<A: Array<Item=u8>> {
len: A::Index,
}
unsafe fn new_array<A: Array<Item=u8>>() -> 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<A: Array<Item=u8>> ArrayString<A> {
/// Create a new empty `ArrayString`.
///
@@ -46,7 +38,7 @@ impl<A: Array<Item=u8>> ArrayString<A> {
pub fn new() -> ArrayString<A> {
unsafe {
ArrayString {
xs: new_array(),
xs: ::new_array(),
len: Index::from(0),
}
}
@@ -150,6 +142,32 @@ impl<A: Array<Item=u8>> Deref for ArrayString<A> {
}
}
impl<A: Array<Item=u8>> PartialEq for ArrayString<A> {
fn eq(&self, rhs: &Self) -> bool {
**self == **rhs
}
}
impl<A: Array<Item=u8>> PartialEq<str> for ArrayString<A> {
fn eq(&self, rhs: &str) -> bool {
&**self == rhs
}
}
impl<A: Array<Item=u8>> PartialEq<ArrayString<A>> for str {
fn eq(&self, rhs: &ArrayString<A>) -> bool {
self == &**rhs
}
}
impl<A: Array<Item=u8>> Eq for ArrayString<A> { }
impl<A: Array<Item=u8>> Hash for ArrayString<A> {
fn hash<H: Hasher>(&self, h: &mut H) {
(**self).hash(h)
}
}
impl<A: Array<Item=u8>> Borrow<str> for ArrayString<A> {
fn borrow(&self) -> &str { self }
}
@@ -158,7 +176,11 @@ impl<A: Array<Item=u8>> AsRef<str> for ArrayString<A> {
fn as_ref(&self) -> &str { self }
}
impl<A: Array<Item=u8>> fmt::Debug for ArrayString<A> where A::Item: fmt::Debug {
impl<A: Array<Item=u8>> fmt::Debug for ArrayString<A> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { (**self).fmt(f) }
}
impl<A: Array<Item=u8>> fmt::Display for ArrayString<A> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { (**self).fmt(f) }
}
@@ -174,4 +196,3 @@ impl<A: Array<Item=u8> + Copy> Clone for ArrayString<A> {
*self
}
}