From 9845491060140f2eec8eb7413736362070b0c00a Mon Sep 17 00:00:00 2001 From: Alexander Regueiro Date: Fri, 5 Feb 2016 22:30:24 +0000 Subject: [PATCH 1/2] Added `ArrayString::from` constructor that takes string slice. Also added corresponding test. --- src/array_string.rs | 20 +++++++++++++++++++- tests/tests.rs | 17 +++++------------ 2 files changed, 24 insertions(+), 13 deletions(-) diff --git a/src/array_string.rs b/src/array_string.rs index a39fbcc..5915bce 100644 --- a/src/array_string.rs +++ b/src/array_string.rs @@ -44,7 +44,25 @@ impl> ArrayString { } } } - + + /// Create a new `ArrayString` from a string slice. + /// + /// Capacity is inferred from the type parameter. + /// + /// ``` + /// use arrayvec::ArrayString; + /// + /// let mut string = ArrayString::<[_; 3]>::from("foo").unwrap(); + /// assert_eq!(&string[..], "foo"); + /// assert_eq!(string.len(), 3); + /// assert_eq!(string.capacity(), 3); + /// ``` + pub fn from(s: &str) -> Result> { + let mut arraystr = Self::new(); + try!(arraystr.push_str(s)); + Ok(arraystr) + } + /// Return the capacity of the `ArrayString`. /// /// ``` diff --git a/tests/tests.rs b/tests/tests.rs index b0b256d..5cfc73d 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -25,17 +25,6 @@ fn test_simple() { assert_eq!(sum_len, 8); } -#[test] -fn test_u16_index() { - const N: usize = 4096; - let mut vec: ArrayVec<[_; N]> = ArrayVec::new(); - for _ in 0..N { - assert!(vec.push(1u8).is_none()); - } - assert!(vec.push(0).is_some()); - assert_eq!(vec.len(), N); -} - #[test] fn test_iter() { let mut iter = ArrayVec::from([1, 2, 3]).into_iter(); @@ -288,7 +277,6 @@ fn test_string() { let tmut: &mut str = &mut t; assert_eq!(tmut, "ab"); - // Test Error trait / try let t = || -> Result<(), Box> { let mut t = ArrayString::<[_; 2]>::new(); @@ -296,4 +284,9 @@ fn test_string() { Ok(()) }(); assert!(t.is_err()); + + // Test `from` constructor + let u = ArrayString::<[_; 11]>::from(text).unwrap(); + assert_eq!(&u, text); + assert_eq!(u.len(), text.len()); } From 0a413975f53c9bda3ad0dbd53ff87747a8077d0c Mon Sep 17 00:00:00 2001 From: Alexander Regueiro Date: Fri, 5 Feb 2016 22:56:44 +0000 Subject: [PATCH 2/2] Re-added accidentally removed test method. Also fixed indentation. --- src/array_string.rs | 4 ++-- tests/tests.rs | 13 ++++++++++++- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/array_string.rs b/src/array_string.rs index 5915bce..4e78652 100644 --- a/src/array_string.rs +++ b/src/array_string.rs @@ -44,7 +44,7 @@ impl> ArrayString { } } } - + /// Create a new `ArrayString` from a string slice. /// /// Capacity is inferred from the type parameter. @@ -62,7 +62,7 @@ impl> ArrayString { try!(arraystr.push_str(s)); Ok(arraystr) } - + /// Return the capacity of the `ArrayString`. /// /// ``` diff --git a/tests/tests.rs b/tests/tests.rs index 5cfc73d..8aa1baa 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -25,6 +25,17 @@ fn test_simple() { assert_eq!(sum_len, 8); } +#[test] +fn test_u16_index() { + const N: usize = 4096; + let mut vec: ArrayVec<[_; N]> = ArrayVec::new(); + for _ in 0..N { + assert!(vec.push(1u8).is_none()); + } + assert!(vec.push(0).is_some()); + assert_eq!(vec.len(), N); +} + #[test] fn test_iter() { let mut iter = ArrayVec::from([1, 2, 3]).into_iter(); @@ -284,7 +295,7 @@ fn test_string() { Ok(()) }(); assert!(t.is_err()); - + // Test `from` constructor let u = ArrayString::<[_; 11]>::from(text).unwrap(); assert_eq!(&u, text);