Add util to assist with Display/to_string for SmallString.

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk
2025-10-28 23:08:14 +00:00
parent 5e89f0acae
commit af0e01e016

View File

@@ -5,7 +5,7 @@ mod unquote;
mod unquoted;
pub use self::{between::Between, split::SplitInfallible, unquote::Unquote, unquoted::Unquoted};
use crate::{Result, utils::exchange};
use crate::{Result, smallstr::SmallString, utils::exchange};
pub const EMPTY: &str = "";
@@ -105,6 +105,18 @@ pub fn common_prefix<'a>(choice: &'a [&str]) -> &'a str {
})
}
pub fn to_small_string<const CAP: usize, T>(t: T) -> SmallString<[u8; CAP]>
where
T: std::fmt::Display,
{
use std::fmt::Write;
let mut ret = SmallString::<[u8; CAP]>::new();
write!(&mut ret, "{t}").expect("Failed to Display type in SmallString");
ret
}
/// Parses the bytes into a string.
pub fn string_from_bytes(bytes: &[u8]) -> Result<String> {
let str: &str = str_from_bytes(bytes)?;