Merge pull request #117 from despawnerer/add-fromstr-implementation
Implement FromStr for ArrayString
This commit is contained in:
@@ -5,6 +5,7 @@ use std::hash::{Hash, Hasher};
|
|||||||
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::FromStr;
|
||||||
use std::str::Utf8Error;
|
use std::str::Utf8Error;
|
||||||
use std::slice;
|
use std::slice;
|
||||||
|
|
||||||
@@ -507,6 +508,16 @@ impl<A> Ord for ArrayString<A>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<A> FromStr for ArrayString<A>
|
||||||
|
where A: Array<Item=u8> + Copy
|
||||||
|
{
|
||||||
|
type Err = CapacityError;
|
||||||
|
|
||||||
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||||
|
Self::from(s).map_err(CapacityError::simplify)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(feature="serde-1")]
|
#[cfg(feature="serde-1")]
|
||||||
/// Requires crate feature `"serde-1"`
|
/// Requires crate feature `"serde-1"`
|
||||||
impl<A> Serialize for ArrayString<A>
|
impl<A> Serialize for ArrayString<A>
|
||||||
|
|||||||
@@ -487,6 +487,14 @@ fn test_string_from() {
|
|||||||
assert_eq!(u.len(), text.len());
|
assert_eq!(u.len(), text.len());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_string_parse_from_str() {
|
||||||
|
let text = "hello world";
|
||||||
|
let u: ArrayString<[_; 11]> = text.parse().unwrap();
|
||||||
|
assert_eq!(&u, text);
|
||||||
|
assert_eq!(u.len(), text.len());
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_string_from_bytes() {
|
fn test_string_from_bytes() {
|
||||||
let text = "hello world";
|
let text = "hello world";
|
||||||
|
|||||||
Reference in New Issue
Block a user