FEAT: Align ArrayString .push and .push_str with String

Use same signatures (meaning: panics on errors). Add fallible versions
.try_push() and .try_push_str()
This commit is contained in:
bluss
2017-07-30 15:38:42 +02:00
parent 345dd33942
commit 1e83039426
2 changed files with 63 additions and 22 deletions
+55 -14
View File
@@ -68,7 +68,7 @@ impl<A: Array<Item=u8>> ArrayString<A> {
/// ``` /// ```
pub fn from(s: &str) -> Result<Self, CapacityError<&str>> { pub fn from(s: &str) -> Result<Self, CapacityError<&str>> {
let mut arraystr = Self::new(); let mut arraystr = Self::new();
try!(arraystr.push_str(s)); arraystr.try_push_str(s)?;
Ok(arraystr) Ok(arraystr)
} }
@@ -84,7 +84,7 @@ impl<A: Array<Item=u8>> ArrayString<A> {
pub fn from_byte_string(b: &A) -> Result<Self, Utf8Error> { pub fn from_byte_string(b: &A) -> Result<Self, Utf8Error> {
let mut arraystr = Self::new(); let mut arraystr = Self::new();
let s = try!(str::from_utf8(b.as_slice())); let s = try!(str::from_utf8(b.as_slice()));
let _result = arraystr.push_str(s); let _result = arraystr.try_push_str(s);
debug_assert!(_result.is_ok()); debug_assert!(_result.is_ok());
Ok(arraystr) Ok(arraystr)
} }
@@ -123,14 +123,34 @@ impl<A: Array<Item=u8>> ArrayString<A> {
/// ///
/// let mut string = ArrayString::<[_; 2]>::new(); /// let mut string = ArrayString::<[_; 2]>::new();
/// ///
/// string.push('a').unwrap(); /// string.push('a');
/// string.push('b').unwrap(); /// string.push('b');
/// let overflow = string.push('c'); ///
/// assert_eq!(&string[..], "ab");
/// ```
pub fn push(&mut self, c: char) {
self.try_push(c).unwrap();
}
/// Adds the given char to the end of the string.
///
/// Returns `Ok` if the push succeeds.
///
/// **Errors** if the backing array is not large enough to fit the additional char.
///
/// ```
/// use arrayvec::ArrayString;
///
/// let mut string = ArrayString::<[_; 2]>::new();
///
/// string.try_push('a').unwrap();
/// string.try_push('b').unwrap();
/// let overflow = string.try_push('c');
/// ///
/// assert_eq!(&string[..], "ab"); /// assert_eq!(&string[..], "ab");
/// assert_eq!(overflow.unwrap_err().element(), 'c'); /// assert_eq!(overflow.unwrap_err().element(), 'c');
/// ``` /// ```
pub fn push(&mut self, c: char) -> Result<(), CapacityError<char>> { pub fn try_push(&mut self, c: char) -> Result<(), CapacityError<char>> {
let len = self.len(); let len = self.len();
unsafe { unsafe {
match encode_utf8(c, &mut self.raw_mut_bytes()[len..]) { match encode_utf8(c, &mut self.raw_mut_bytes()[len..]) {
@@ -154,16 +174,36 @@ impl<A: Array<Item=u8>> ArrayString<A> {
/// ///
/// let mut string = ArrayString::<[_; 2]>::new(); /// let mut string = ArrayString::<[_; 2]>::new();
/// ///
/// string.push_str("a").unwrap(); /// string.push_str("a");
/// let overflow1 = string.push_str("bc"); /// string.push_str("d");
/// string.push_str("d").unwrap(); ///
/// let overflow2 = string.push_str("ef"); /// assert_eq!(&string[..], "ad");
/// ```
pub fn push_str(&mut self, s: &str) {
self.try_push_str(s).unwrap()
}
/// Adds the given string slice to the end of the string.
///
/// Returns `Ok` if the push succeeds.
///
/// **Errors** if the backing array is not large enough to fit the string.
///
/// ```
/// use arrayvec::ArrayString;
///
/// let mut string = ArrayString::<[_; 2]>::new();
///
/// string.try_push_str("a").unwrap();
/// let overflow1 = string.try_push_str("bc");
/// string.try_push_str("d").unwrap();
/// let overflow2 = string.try_push_str("ef");
/// ///
/// assert_eq!(&string[..], "ad"); /// assert_eq!(&string[..], "ad");
/// assert_eq!(overflow1.unwrap_err().element(), "bc"); /// assert_eq!(overflow1.unwrap_err().element(), "bc");
/// assert_eq!(overflow2.unwrap_err().element(), "ef"); /// assert_eq!(overflow2.unwrap_err().element(), "ef");
/// ``` /// ```
pub fn push_str<'a>(&mut self, s: &'a str) -> Result<(), CapacityError<&'a str>> { pub fn try_push_str<'a>(&mut self, s: &'a str) -> Result<(), CapacityError<&'a str>> {
if s.len() > self.capacity() - self.len() { if s.len() > self.capacity() - self.len() {
return Err(CapacityError::new(s)); return Err(CapacityError::new(s));
} }
@@ -274,10 +314,11 @@ impl<A: Array<Item=u8>> fmt::Display for ArrayString<A> {
/// `Write` appends written data to the end of the string. /// `Write` appends written data to the end of the string.
impl<A: Array<Item=u8>> fmt::Write for ArrayString<A> { impl<A: Array<Item=u8>> fmt::Write for ArrayString<A> {
fn write_char(&mut self, c: char) -> fmt::Result { fn write_char(&mut self, c: char) -> fmt::Result {
self.push(c).map_err(|_| fmt::Error) self.try_push(c).map_err(|_| fmt::Error)
} }
fn write_str(&mut self, s: &str) -> fmt::Result { fn write_str(&mut self, s: &str) -> fmt::Result {
self.push_str(s).map_err(|_| fmt::Error) self.try_push_str(s).map_err(|_| fmt::Error)
} }
} }
@@ -288,7 +329,7 @@ impl<A: Array<Item=u8> + Copy> Clone for ArrayString<A> {
fn clone_from(&mut self, rhs: &Self) { fn clone_from(&mut self, rhs: &Self) {
// guaranteed to fit due to types matching. // guaranteed to fit due to types matching.
self.clear(); self.clear();
self.push_str(rhs).ok(); self.try_push_str(rhs).ok();
} }
} }
+8 -8
View File
@@ -307,7 +307,7 @@ fn test_string() {
let text = "hello world"; let text = "hello world";
let mut s = ArrayString::<[_; 16]>::new(); let mut s = ArrayString::<[_; 16]>::new();
s.push_str(text).unwrap(); s.try_push_str(text).unwrap();
assert_eq!(&s, text); assert_eq!(&s, text);
assert_eq!(text, &s); assert_eq!(text, &s);
@@ -317,10 +317,10 @@ fn test_string() {
assert_eq!(map[text], 1); assert_eq!(map[text], 1);
let mut t = ArrayString::<[_; 2]>::new(); let mut t = ArrayString::<[_; 2]>::new();
assert!(t.push_str(text).is_err()); assert!(t.try_push_str(text).is_err());
assert_eq!(&t, ""); assert_eq!(&t, "");
t.push_str("ab").unwrap(); t.push_str("ab");
// DerefMut // DerefMut
let tmut: &mut str = &mut t; let tmut: &mut str = &mut t;
assert_eq!(tmut, "ab"); assert_eq!(tmut, "ab");
@@ -328,7 +328,7 @@ fn test_string() {
// Test Error trait / try // Test Error trait / try
let t = || -> Result<(), Box<Error>> { let t = || -> Result<(), Box<Error>> {
let mut t = ArrayString::<[_; 2]>::new(); let mut t = ArrayString::<[_; 2]>::new();
try!(t.push_str(text)); try!(t.try_push_str(text));
Ok(()) Ok(())
}(); }();
assert!(t.is_err()); assert!(t.is_err());
@@ -355,7 +355,7 @@ fn test_string_from_bytes() {
fn test_string_clone() { fn test_string_clone() {
let text = "hi"; let text = "hi";
let mut s = ArrayString::<[_; 4]>::new(); let mut s = ArrayString::<[_; 4]>::new();
s.push_str("abcd").unwrap(); s.push_str("abcd");
let t = ArrayString::<[_; 4]>::from(text).unwrap(); let t = ArrayString::<[_; 4]>::from(text).unwrap();
s.clone_from(&t); s.clone_from(&t);
assert_eq!(&t, &s); assert_eq!(&t, &s);
@@ -366,14 +366,14 @@ fn test_string_push() {
let text = "abcαβγ"; let text = "abcαβγ";
let mut s = ArrayString::<[_; 8]>::new(); let mut s = ArrayString::<[_; 8]>::new();
for c in text.chars() { for c in text.chars() {
if let Err(_) = s.push(c) { if let Err(_) = s.try_push(c) {
break; break;
} }
} }
assert_eq!("abcαβ", &s[..]); assert_eq!("abcαβ", &s[..]);
s.push('x').ok(); s.push('x');
assert_eq!("abcαβx", &s[..]); assert_eq!("abcαβx", &s[..]);
assert!(s.push('x').is_err()); assert!(s.try_push('x').is_err());
} }