Add DerefMut for ArrayString
This commit is contained in:
+13
-1
@@ -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
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user