Created
June 6, 2018 13:49
-
-
Save mitsuhiko/599b856709cc4d4d1541be131aecb209 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::fmt::Debug; | |
trait Foo: Debug + 'static { | |
// either remove this method | |
fn debug(&self) -> String where Self: Sized { | |
format!("{:?}", self) | |
} | |
} | |
impl Foo { | |
fn debug(&self) -> String { | |
format!("{:?}", self) | |
} | |
} | |
trait AsFoo { | |
fn as_foo(&self) -> &Foo; | |
} | |
impl<T: Bar> AsFoo for T { | |
fn as_foo(&self) -> &Foo { self } | |
} | |
trait Bar: Foo + AsFoo {} | |
impl Bar { | |
fn debug_via_bar(&self) { | |
println!("debug: {}", self.as_foo().debug()) | |
} | |
} | |
impl Foo for i32 {} | |
impl Bar for i32 {} | |
fn main() { | |
let x: &Bar = &42i32; | |
// or remove this call. | |
x.debug_via_bar(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment