use core::fmt;
use crate::error::Error;
use crate::fmt::TryWrite;
use crate::string::String;
#[cfg(test)]
use crate::testing::*;
pub trait TryToString {
#[cfg(test)]
fn to_string(&self) -> String {
self.try_to_string().abort()
}
fn try_to_string(&self) -> Result<String, Error>;
}
impl<T> TryToString for T
where
T: fmt::Display,
{
#[inline]
fn try_to_string(&self) -> Result<String, Error> {
let mut s = String::new();
core::write!(s, "{}", self)?;
Ok(s)
}
}
impl TryToString for str {
#[inline]
fn try_to_string(&self) -> Result<String, Error> {
String::try_from(self)
}
}