impl TryFrom<&'a str> for ArrayString

This commit is contained in:
Caio
2020-08-28 08:16:20 -03:00
parent 4043c58de7
commit 2267276dbb
+16 -2
View File
@@ -1,13 +1,14 @@
use std::borrow::Borrow; use std::borrow::Borrow;
use std::cmp; use std::cmp;
use std::convert::TryFrom;
use std::fmt; use std::fmt;
use std::hash::{Hash, Hasher}; use std::hash::{Hash, Hasher};
use std::ptr;
use std::ops::{Deref, DerefMut}; use std::ops::{Deref, DerefMut};
use std::ptr;
use std::slice;
use std::str; use std::str;
use std::str::FromStr; use std::str::FromStr;
use std::str::Utf8Error; use std::str::Utf8Error;
use std::slice;
use crate::array::Array; use crate::array::Array;
use crate::array::Index; use crate::array::Index;
@@ -569,3 +570,16 @@ impl<'de, A> Deserialize<'de> for ArrayString<A>
deserializer.deserialize_str(ArrayStringVisitor::<A>(PhantomData)) deserializer.deserialize_str(ArrayStringVisitor::<A>(PhantomData))
} }
} }
impl<'a, A> TryFrom<&'a str> for ArrayString<A>
where
A: Array<Item = u8> + Copy
{
type Error = CapacityError<&'a str>;
fn try_from(f: &'a str) -> Result<Self, Self::Error> {
let mut v = Self::new();
v.try_push_str(f)?;
Ok(v)
}
}