Add constructor ArrayString::from_byte_string(b"abc")
This is an alternative constructor that never has capacity errors. Unfortunately the error case is invalid UTF-8!
This commit is contained in:
@@ -19,6 +19,19 @@ pub trait Index : PartialEq + Copy {
|
|||||||
fn from(usize) -> Self;
|
fn from(usize) -> Self;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
use std::slice::{from_raw_parts};
|
||||||
|
|
||||||
|
pub trait ArrayExt : Array {
|
||||||
|
#[inline(always)]
|
||||||
|
fn as_slice(&self) -> &[Self::Item] {
|
||||||
|
unsafe {
|
||||||
|
from_raw_parts(self.as_ptr(), Self::capacity())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<A> ArrayExt for A where A: Array { }
|
||||||
|
|
||||||
#[cfg(feature = "use_generic_array")]
|
#[cfg(feature = "use_generic_array")]
|
||||||
unsafe impl<T, U> Array for ::generic_array::GenericArray<T, U>
|
unsafe impl<T, U> Array for ::generic_array::GenericArray<T, U>
|
||||||
where U: ::generic_array::ArrayLength<T>
|
where U: ::generic_array::ArrayLength<T>
|
||||||
|
|||||||
+19
-1
@@ -5,9 +5,10 @@ use std::mem;
|
|||||||
use std::ptr;
|
use std::ptr;
|
||||||
use std::ops::{Deref, DerefMut};
|
use std::ops::{Deref, DerefMut};
|
||||||
use std::str;
|
use std::str;
|
||||||
|
use std::str::Utf8Error;
|
||||||
use std::slice;
|
use std::slice;
|
||||||
|
|
||||||
use array::Array;
|
use array::{Array, ArrayExt};
|
||||||
use array::Index;
|
use array::Index;
|
||||||
use CapacityError;
|
use CapacityError;
|
||||||
use odds::char::encode_utf8;
|
use odds::char::encode_utf8;
|
||||||
@@ -67,6 +68,23 @@ impl<A: Array<Item=u8>> ArrayString<A> {
|
|||||||
Ok(arraystr)
|
Ok(arraystr)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Create a new `ArrayString` from a byte string literal.
|
||||||
|
///
|
||||||
|
/// **Errors** if the byte string literal is not valid UTF-8.
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// use arrayvec::ArrayString;
|
||||||
|
///
|
||||||
|
/// let string = ArrayString::from_byte_string(b"hello world").unwrap();
|
||||||
|
/// ```
|
||||||
|
pub fn from_byte_string(b: &A) -> Result<Self, Utf8Error> {
|
||||||
|
let mut arraystr = Self::new();
|
||||||
|
let s = try!(str::from_utf8(b.as_slice()));
|
||||||
|
let _result = arraystr.push_str(s);
|
||||||
|
debug_assert!(_result.is_ok());
|
||||||
|
Ok(arraystr)
|
||||||
|
}
|
||||||
|
|
||||||
/// Return the capacity of the `ArrayString`.
|
/// Return the capacity of the `ArrayString`.
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
|
|||||||
@@ -346,6 +346,14 @@ fn test_string_from() {
|
|||||||
assert_eq!(u.len(), text.len());
|
assert_eq!(u.len(), text.len());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_string_from_bytes() {
|
||||||
|
let text = "hello world";
|
||||||
|
let u = ArrayString::from_byte_string(b"hello world").unwrap();
|
||||||
|
assert_eq!(&u, text);
|
||||||
|
assert_eq!(u.len(), text.len());
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_string_clone() {
|
fn test_string_clone() {
|
||||||
let text = "hi";
|
let text = "hi";
|
||||||
|
|||||||
Reference in New Issue
Block a user