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 62 63 64
use std::fmt::{ Display, Debug }; use super::any::*; pub trait ToStringExtension { fn to_string(&self) -> String; } pub trait ToDebugString { fn to_debug_string(&self) -> String; } pub trait ToInstanceString { fn to_instance_string(&self) -> String; } impl<T: ?Sized> ToInstanceString for T { fn to_instance_string(&self) -> String { format!("{{ type_name: {}, memory_address: {} }}", self.type_name(), self.memory_address()) } } impl<T: ?Sized + Debug> ToDebugString for T { fn to_debug_string(&self) -> String { format!("{:?}", self) } } impl<T: Display> ToStringExtension for Option<T> { fn to_string(&self) -> String { match self { Some(value) => format!("Some({})", value.to_string()), None => "None".to_string() } } } #[cfg(test)] mod tests { use super::*; #[test] fn to_string() { let mut o = Some(10); println!("{}", o.to_string()); o = None; println!("{}", o.to_string()); } #[test] fn to_debug_string() { let mut o = Some(10); println!("{}", o.to_debug_string()); println!("{:?}", o); o = None; println!("{}", o.to_debug_string()); } #[test] fn to_instance_string() { let o = Some(10); println!("{}", o.to_instance_string()); } }