Add DerefMut for ArrayString

This commit is contained in:
bluss
2016-01-13 12:59:06 +01:00
parent 2046e3f726
commit 62d372ce1f
2 changed files with 19 additions and 1 deletions
+13 -1
View File
@@ -1,7 +1,8 @@
use std::borrow::Borrow;
use std::fmt;
use std::hash::{Hash, Hasher};
use std::ops::Deref;
use std::mem;
use std::ops::{Deref, DerefMut};
use std::str;
use std::slice;
@@ -142,6 +143,17 @@ impl<A: Array<Item=u8>> Deref for ArrayString<A> {
}
}
impl<A: Array<Item=u8>> DerefMut for ArrayString<A> {
#[inline]
fn deref_mut(&mut self) -> &mut str {
unsafe {
let sl = slice::from_raw_parts_mut(self.xs.as_mut_ptr(), self.len.to_usize());
// FIXME: Nothing but transmute to do this right now
mem::transmute(sl)
}
}
}
impl<A: Array<Item=u8>> PartialEq for ArrayString<A> {
fn eq(&self, rhs: &Self) -> bool {
**self == **rhs
+6
View File
@@ -283,6 +283,12 @@ fn test_string() {
assert!(t.push_str(text).is_err());
assert_eq!(&t, "");
t.push_str("ab").unwrap();
// DerefMut
let tmut: &mut str = &mut t;
assert_eq!(tmut, "ab");
// Test Error trait / try
let t = || -> Result<(), Box<Error>> {
let mut t = ArrayString::<[_; 2]>::new();