diff --git a/src/array_string.rs b/src/array_string.rs index af09023..51a4c16 100644 --- a/src/array_string.rs +++ b/src/array_string.rs @@ -68,7 +68,7 @@ impl> ArrayString { /// ``` pub fn from(s: &str) -> Result> { let mut arraystr = Self::new(); - try!(arraystr.push_str(s)); + arraystr.try_push_str(s)?; Ok(arraystr) } @@ -84,7 +84,7 @@ impl> ArrayString { pub fn from_byte_string(b: &A) -> Result { let mut arraystr = Self::new(); 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()); Ok(arraystr) } @@ -123,14 +123,34 @@ impl> ArrayString { /// /// let mut string = ArrayString::<[_; 2]>::new(); /// - /// string.push('a').unwrap(); - /// string.push('b').unwrap(); - /// let overflow = string.push('c'); + /// string.push('a'); + /// string.push('b'); + /// + /// 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!(overflow.unwrap_err().element(), 'c'); /// ``` - pub fn push(&mut self, c: char) -> Result<(), CapacityError> { + pub fn try_push(&mut self, c: char) -> Result<(), CapacityError> { let len = self.len(); unsafe { match encode_utf8(c, &mut self.raw_mut_bytes()[len..]) { @@ -154,16 +174,36 @@ impl> ArrayString { /// /// let mut string = ArrayString::<[_; 2]>::new(); /// - /// string.push_str("a").unwrap(); - /// let overflow1 = string.push_str("bc"); - /// string.push_str("d").unwrap(); - /// let overflow2 = string.push_str("ef"); + /// string.push_str("a"); + /// string.push_str("d"); + /// + /// 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!(overflow1.unwrap_err().element(), "bc"); /// 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() { return Err(CapacityError::new(s)); } @@ -274,10 +314,11 @@ impl> fmt::Display for ArrayString { /// `Write` appends written data to the end of the string. impl> fmt::Write for ArrayString { 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 { - self.push_str(s).map_err(|_| fmt::Error) + self.try_push_str(s).map_err(|_| fmt::Error) } } @@ -288,7 +329,7 @@ impl + Copy> Clone for ArrayString { fn clone_from(&mut self, rhs: &Self) { // guaranteed to fit due to types matching. self.clear(); - self.push_str(rhs).ok(); + self.try_push_str(rhs).ok(); } } diff --git a/tests/tests.rs b/tests/tests.rs index 19b95c2..0787f68 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -307,7 +307,7 @@ fn test_string() { let text = "hello world"; let mut s = ArrayString::<[_; 16]>::new(); - s.push_str(text).unwrap(); + s.try_push_str(text).unwrap(); assert_eq!(&s, text); assert_eq!(text, &s); @@ -317,10 +317,10 @@ fn test_string() { assert_eq!(map[text], 1); let mut t = ArrayString::<[_; 2]>::new(); - assert!(t.push_str(text).is_err()); + assert!(t.try_push_str(text).is_err()); assert_eq!(&t, ""); - t.push_str("ab").unwrap(); + t.push_str("ab"); // DerefMut let tmut: &mut str = &mut t; assert_eq!(tmut, "ab"); @@ -328,7 +328,7 @@ fn test_string() { // Test Error trait / try let t = || -> Result<(), Box> { let mut t = ArrayString::<[_; 2]>::new(); - try!(t.push_str(text)); + try!(t.try_push_str(text)); Ok(()) }(); assert!(t.is_err()); @@ -355,7 +355,7 @@ fn test_string_from_bytes() { fn test_string_clone() { let text = "hi"; let mut s = ArrayString::<[_; 4]>::new(); - s.push_str("abcd").unwrap(); + s.push_str("abcd"); let t = ArrayString::<[_; 4]>::from(text).unwrap(); s.clone_from(&t); assert_eq!(&t, &s); @@ -366,14 +366,14 @@ fn test_string_push() { let text = "abcαβγ"; let mut s = ArrayString::<[_; 8]>::new(); for c in text.chars() { - if let Err(_) = s.push(c) { + if let Err(_) = s.try_push(c) { break; } } assert_eq!("abcαβ", &s[..]); - s.push('x').ok(); + s.push('x'); assert_eq!("abcαβx", &s[..]); - assert!(s.push('x').is_err()); + assert!(s.try_push('x').is_err()); }