ArrayString: Make Copy, don't use ArrayVec

This commit is contained in:
bluss
2015-09-12 14:34:11 +02:00
parent 078dbf4b15
commit 10aa8245d8
+40 -19
View File
@@ -3,10 +3,10 @@ use std::fmt;
use std::mem; use std::mem;
use std::ops::Deref; use std::ops::Deref;
use std::str; use std::str;
use std::slice;
use ArrayVec;
use array::Array; use array::Array;
use array::Index;
/// A string with a fixed capacity. /// A string with a fixed capacity.
/// ///
@@ -18,8 +18,18 @@ use array::Array;
/// ///
/// Due to technical restrictions, this struct does not implement `Copy` even /// Due to technical restrictions, this struct does not implement `Copy` even
/// though it would be safe to do so. /// though it would be safe to do so.
#[derive(Copy)]
pub struct ArrayString<A: Array<Item=u8>> { pub struct ArrayString<A: Array<Item=u8>> {
vec: ArrayVec<A>, xs: A,
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> { impl<A: Array<Item=u8>> ArrayString<A> {
@@ -36,8 +46,11 @@ impl<A: Array<Item=u8>> ArrayString<A> {
/// assert_eq!(string.capacity(), 16); /// assert_eq!(string.capacity(), 16);
/// ``` /// ```
pub fn new() -> ArrayString<A> { pub fn new() -> ArrayString<A> {
unsafe {
ArrayString { ArrayString {
vec: ArrayVec::new() xs: new_array(),
len: Index::from(0),
}
} }
} }
@@ -50,12 +63,12 @@ impl<A: Array<Item=u8>> ArrayString<A> {
/// assert_eq!(string.capacity(), 3); /// assert_eq!(string.capacity(), 3);
/// ``` /// ```
#[inline] #[inline]
pub fn capacity(&self) -> usize { self.vec.capacity() } pub fn capacity(&self) -> usize { A::capacity() }
/// Adds the given character to the end of the string. /// Adds the given char to the end of the string.
/// ///
/// Returns `None` if the push succeeds, or and returns `Some(c)` if the /// Returns `None` if the push succeeds, or and returns `Some(c)` if the
/// backing array is not large enough to fit the additional character. /// backing array is not large enough to fit the additional char.
/// ///
/// ``` /// ```
/// use arrayvec::ArrayString; /// use arrayvec::ArrayString;
@@ -98,20 +111,25 @@ impl<A: Array<Item=u8>> ArrayString<A> {
/// assert_eq!(overflow2, Some("ef")); /// assert_eq!(overflow2, Some("ef"));
/// ``` /// ```
pub fn push_str<'a>(&mut self, s: &'a str) -> Option<&'a str> { pub fn push_str<'a>(&mut self, s: &'a str) -> Option<&'a str> {
use std::io::Write;
if self.len() + s.len() > self.capacity() { if self.len() + s.len() > self.capacity() {
return Some(s); return Some(s);
} }
let mut bytes = s.bytes(); unsafe {
self.vec.extend(&mut bytes); let sl = slice::from_raw_parts_mut(self.xs.as_mut_ptr(), A::capacity());
assert!(bytes.next().is_none()); (&mut sl[self.len()..]).write(s.as_bytes()).unwrap();
let newl = self.len() + s.len();
self.set_len(newl);
}
None None
} }
/// Make the string empty. /// Make the string empty.
pub fn clear(&mut self) { pub fn clear(&mut self) {
mem::replace(self, ArrayString::new()); unsafe {
self.set_len(0);
}
} }
/// Set the strings's length. /// Set the strings's length.
@@ -119,10 +137,11 @@ impl<A: Array<Item=u8>> ArrayString<A> {
/// May panic if `length` is greater than the capacity. /// May panic if `length` is greater than the capacity.
/// ///
/// This function is `unsafe` because it changes the notion of the /// This function is `unsafe` because it changes the notion of the
/// number of “valid” characters in the string. Use with care. /// number of “valid” bytes in the string. Use with care.
#[inline] #[inline]
pub unsafe fn set_len(&mut self, length: usize) { pub unsafe fn set_len(&mut self, length: usize) {
self.vec.set_len(length) debug_assert!(length <= self.capacity());
self.len = Index::from(length);
} }
} }
@@ -130,7 +149,10 @@ impl<A: Array<Item=u8>> Deref for ArrayString<A> {
type Target = str; type Target = str;
#[inline] #[inline]
fn deref(&self) -> &str { fn deref(&self) -> &str {
unsafe { str::from_utf8_unchecked(&self.vec) } unsafe {
let sl = slice::from_raw_parts(self.xs.as_ptr(), self.len.to_usize());
str::from_utf8_unchecked(sl)
}
} }
} }
@@ -156,10 +178,9 @@ impl<A: Array<Item=u8>> fmt::Write for ArrayString<A> {
} }
} }
//#[derive(Clone, /*Copy,*/ Eq, Hash, Ord, PartialEq, PartialOrd)] impl<A: Array<Item=u8> + Copy> Clone for ArrayString<A> {
impl<A: Array<Item=u8>> Clone for ArrayString<A> {
fn clone(&self) -> ArrayString<A> { fn clone(&self) -> ArrayString<A> {
ArrayString { vec: self.vec.clone() } *self
} }
} }