diff --git a/src/array_string.rs b/src/array_string.rs index 65a8a02..a39fbcc 100644 --- a/src/array_string.rs +++ b/src/array_string.rs @@ -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> Deref for ArrayString { } } +impl> DerefMut for ArrayString { + #[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> PartialEq for ArrayString { fn eq(&self, rhs: &Self) -> bool { **self == **rhs diff --git a/tests/tests.rs b/tests/tests.rs index 3dd24bd..b0b256d 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -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> { let mut t = ArrayString::<[_; 2]>::new();