Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::fmt::{self,Display,Formatter,Write};
- use std::string::ToString;
- trait Format {
- fn fmt(&self, f: &mut Formatter) -> fmt::Result;
- }
- impl<'a> Display for Format + 'a {
- fn fmt(&self, f: &mut Formatter) -> fmt::Result {
- Format::fmt(self, f)
- }
- }
- impl<'a, T: Format> Format for &'a T {
- fn fmt(&self, f: &mut Formatter) -> fmt::Result {
- Ok(())
- }
- }
- struct Foo<T>(pub Option<T>);
- impl<T: Format> Format for Foo<T> {
- fn fmt(&self, f: &mut Formatter) -> fmt::Result {
- Ok(())
- }
- }
- struct Bar { bar: usize }
- impl Format for Bar {
- fn fmt(&self, f: &mut Formatter) -> fmt::Result {
- f.write_str("bar")
- }
- }
- fn main() {
- let to_string = <Format as ToString>::to_string;
- let i = Bar { bar: 1 };
- let foo = Foo(Some(&i));
- assert_eq!("bar", to_string(&foo));
- }
Add Comment
Please, Sign In to add comment