rune_alloc/string/
try_to_string.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//! String utilities.

use core::fmt;

use crate::error::Error;
use crate::fmt::TryWrite;
use crate::string::String;
#[cfg(test)]
use crate::testing::*;

/// A trait for converting a value to a `String`.
///
/// This trait is automatically implemented for any type which implements the
/// [`Display`] trait. As such, `ToString` shouldn't be implemented directly:
/// [`Display`] should be implemented instead, and you get the `ToString`
/// implementation for free.
///
/// [`Display`]: core::fmt::Display
pub trait TryToString {
    #[cfg(test)]
    fn to_string(&self) -> String {
        self.try_to_string().abort()
    }

    /// Converts the given value to a `String`.
    ///
    /// # Examples
    ///
    /// Basic usage:
    ///
    /// ```
    /// use rune::alloc::String;
    /// use rune::alloc::prelude::*;
    ///
    /// let i = 5;
    /// let five = String::try_from("5")?;
    ///
    /// assert_eq!(five, i.try_to_string()?);
    /// # Ok::<_, rune::alloc::Error>(())
    /// ```
    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)
    }
}