Added ArrayString::from constructor that takes string slice.
Also added corresponding test.
This commit is contained in:
@@ -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`.
|
/// Return the capacity of the `ArrayString`.
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
|
|||||||
+5
-12
@@ -25,17 +25,6 @@ fn test_simple() {
|
|||||||
assert_eq!(sum_len, 8);
|
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]
|
#[test]
|
||||||
fn test_iter() {
|
fn test_iter() {
|
||||||
let mut iter = ArrayVec::from([1, 2, 3]).into_iter();
|
let mut iter = ArrayVec::from([1, 2, 3]).into_iter();
|
||||||
@@ -288,7 +277,6 @@ fn test_string() {
|
|||||||
let tmut: &mut str = &mut t;
|
let tmut: &mut str = &mut t;
|
||||||
assert_eq!(tmut, "ab");
|
assert_eq!(tmut, "ab");
|
||||||
|
|
||||||
|
|
||||||
// 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();
|
||||||
@@ -296,4 +284,9 @@ fn test_string() {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}();
|
}();
|
||||||
assert!(t.is_err());
|
assert!(t.is_err());
|
||||||
|
|
||||||
|
// Test `from` constructor
|
||||||
|
let u = ArrayString::<[_; 11]>::from(text).unwrap();
|
||||||
|
assert_eq!(&u, text);
|
||||||
|
assert_eq!(u.len(), text.len());
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user