Guest User

Untitled

a guest
Mar 24th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.81 KB | None | 0 0
  1. use std::fmt::{self,Display,Formatter,Write};
  2. use std::string::ToString;
  3.  
  4. trait Format {
  5. fn fmt(&self, f: &mut Formatter) -> fmt::Result;
  6. }
  7.  
  8. impl<'a> Display for Format + 'a {
  9. fn fmt(&self, f: &mut Formatter) -> fmt::Result {
  10. Format::fmt(self, f)
  11. }
  12. }
  13.  
  14. impl<'a, T: Format> Format for &'a T {
  15. fn fmt(&self, f: &mut Formatter) -> fmt::Result {
  16. Ok(())
  17. }
  18. }
  19.  
  20. struct Foo<T>(pub Option<T>);
  21.  
  22. impl<T: Format> Format for Foo<T> {
  23. fn fmt(&self, f: &mut Formatter) -> fmt::Result {
  24. Ok(())
  25. }
  26. }
  27.  
  28. struct Bar { bar: usize }
  29.  
  30. impl Format for Bar {
  31. fn fmt(&self, f: &mut Formatter) -> fmt::Result {
  32. f.write_str("bar")
  33. }
  34. }
  35.  
  36. fn main() {
  37. let to_string = <Format as ToString>::to_string;
  38. let i = Bar { bar: 1 };
  39. let foo = Foo(Some(&i));
  40. assert_eq!("bar", to_string(&foo));
  41. }
Add Comment
Please, Sign In to add comment