From 2267276dbb825fdac7ebbcb551aa98b670915bc1 Mon Sep 17 00:00:00 2001 From: Caio Date: Fri, 28 Aug 2020 08:16:20 -0300 Subject: [PATCH] impl TryFrom<&'a str> for ArrayString --- src/array_string.rs | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/array_string.rs b/src/array_string.rs index 46f4675..f7343b9 100644 --- a/src/array_string.rs +++ b/src/array_string.rs @@ -1,13 +1,14 @@ use std::borrow::Borrow; use std::cmp; +use std::convert::TryFrom; use std::fmt; use std::hash::{Hash, Hasher}; -use std::ptr; use std::ops::{Deref, DerefMut}; +use std::ptr; +use std::slice; use std::str; use std::str::FromStr; use std::str::Utf8Error; -use std::slice; use crate::array::Array; use crate::array::Index; @@ -569,3 +570,16 @@ impl<'de, A> Deserialize<'de> for ArrayString deserializer.deserialize_str(ArrayStringVisitor::(PhantomData)) } } + +impl<'a, A> TryFrom<&'a str> for ArrayString +where + A: Array + Copy +{ + type Error = CapacityError<&'a str>; + + fn try_from(f: &'a str) -> Result { + let mut v = Self::new(); + v.try_push_str(f)?; + Ok(v) + } +}