Merge pull request #165 from c410-f3r/try

impl TryFrom<&'a str> for ArrayString
This commit is contained in:
bluss
2020-12-01 02:22:36 +01:00
committed by GitHub
+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;
@@ -580,3 +581,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)
}
}