Skip to content

Instantly share code, notes, and snippets.

@mitsuhiko
Created June 6, 2018 13:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mitsuhiko/599b856709cc4d4d1541be131aecb209 to your computer and use it in GitHub Desktop.
Save mitsuhiko/599b856709cc4d4d1541be131aecb209 to your computer and use it in GitHub Desktop.
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