lang_extension/fmt/
mod.rs

1use std::fmt::{Debug, Display};
2
3use super::any::*;
4
5pub trait ToStringExtension {
6    fn to_string(&self) -> String;
7}
8
9pub trait ToDebugString {
10    fn to_debug_string(&self) -> String;
11}
12
13pub trait ToInstanceString {
14    fn to_instance_string(&self) -> String;
15}
16
17impl<T: ?Sized> ToInstanceString for T {
18    fn to_instance_string(&self) -> String {
19        format!(
20            "{{ type_name: {}, memory_address: {} }}",
21            self.type_name(),
22            self.memory_address()
23        )
24    }
25}
26
27impl<T: ?Sized + Debug> ToDebugString for T {
28    fn to_debug_string(&self) -> String {
29        format!("{:?}", self)
30    }
31}
32
33impl<T: Display> ToStringExtension for Option<T> {
34    fn to_string(&self) -> String {
35        match self {
36            Some(value) => format!("Some({})", value.to_string()),
37            None => "None".to_string(),
38        }
39    }
40}
41
42#[cfg(test)]
43mod tests {
44    use super::*;
45
46    #[test]
47    fn to_string() {
48        let mut o = Some(10);
49        println!("{}", o.to_string());
50        o = None;
51        println!("{}", o.to_string());
52    }
53
54    #[test]
55    fn to_debug_string() {
56        let mut o = Some(10);
57        println!("{}", o.to_debug_string());
58        println!("{:?}", o);
59        o = None;
60        println!("{}", o.to_debug_string());
61    }
62
63    #[test]
64    fn to_instance_string() {
65        let o = Some(10);
66        println!("{}", o.to_instance_string());
67    }
68}