kx_utils/experimental/
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
//
#![allow(unused)]

/// Converts the parameter into a [String] using the [Into] trait.
///
/// Requires parameter to implement a [From] or [Into] trait to allow it be transformed.
pub fn s(v: impl Into<String>) -> String {
    v.into()
}

/// Converts the parameter into a [String] by calling `.to_string()`.
///
/// Unlike the [s] function, the value only need to have a [to_string] method. Preusmably it will
/// return a [String], but this macro doesn't make that gaurantee.
#[macro_export]
macro_rules! s {
    ($value:expr) => {
        $value.to_string()
    };
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn s_converts_to_string() {
        let v = 123.to_string();
        let result = s("123");
        assert_eq!(result, v);
    }
}