Merge pull request #23 from alexreg/master

Added `ArrayString::from` constructor that takes string slice.
This commit is contained in:
bluss
2016-02-06 00:57:03 +01:00
2 changed files with 23 additions and 1 deletions
+18
View File
@@ -45,6 +45,24 @@ impl<A: Array<Item=u8>> ArrayString<A> {
}
}
/// 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<Self, CapacityError<&str>> {
let mut arraystr = Self::new();
try!(arraystr.push_str(s));
Ok(arraystr)
}
/// Return the capacity of the `ArrayString`.
///
/// ```
+5 -1
View File
@@ -288,7 +288,6 @@ fn test_string() {
let tmut: &mut str = &mut t;
assert_eq!(tmut, "ab");
// Test Error trait / try
let t = || -> Result<(), Box<Error>> {
let mut t = ArrayString::<[_; 2]>::new();
@@ -296,4 +295,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());
}