From e43693dd6c72b0b7c0976a3c598e5c20a0c8e932 Mon Sep 17 00:00:00 2001 From: GnomedDev Date: Sun, 10 Mar 2024 18:26:16 +0000 Subject: [PATCH] Add FromStr implementation for FixedString --- src/string.rs | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/string.rs b/src/string.rs index 7e1fd3a..3e174fa 100644 --- a/src/string.rs +++ b/src/string.rs @@ -4,7 +4,7 @@ use alloc::{ string::{String, ToString}, sync::Arc, }; -use core::{borrow::Borrow, fmt::Write as _, hash::Hash}; +use core::{borrow::Borrow, fmt::Write as _, hash::Hash, str::FromStr}; use crate::{ array::FixedArray, @@ -248,6 +248,18 @@ impl core::fmt::Debug for FixedString { } } +impl FromStr for FixedString { + type Err = InvalidStrLength; + + fn from_str(val: &str) -> Result { + if let Some(inline) = Self::new_inline(val) { + Ok(inline) + } else { + Self::try_from(Box::from(val)) + } + } +} + impl TryFrom> for FixedString { type Error = InvalidStrLength; @@ -329,11 +341,7 @@ impl<'de, LenT: ValidLength> serde::Deserialize<'de> for FixedString { } fn visit_str(self, val: &str) -> Result { - if let Some(inline) = FixedString::new_inline(val) { - Ok(inline) - } else { - FixedString::try_from(Box::from(val)).map_err(E::custom) - } + FixedString::from_str(val).map_err(E::custom) } fn visit_string(self, val: String) -> Result {