Skip to content

Instantly share code, notes, and snippets.

@alexreg
Created October 7, 2019 15:51
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 alexreg/2e47ba39c1bd5b3a4dcfff5d2e5660e5 to your computer and use it in GitHub Desktop.
Save alexreg/2e47ba39c1bd5b3a4dcfff5d2e5660e5 to your computer and use it in GitHub Desktop.
// #![feature(trait_upcasting)]
use std::sync::Arc;
trait Foo: std::fmt::Display {
fn a(&self) {
println!("Foo::a -- {}", self);
}
}
trait Bar: Foo {
fn a(&self) {
println!("Bar::a -- {}", self);
}
fn b(&self) {
println!("Bar::b -- {}", self);
}
}
impl Foo for i32 {}
impl Bar for i32 {}
fn main() {
let bar: &dyn Bar = &21;
let foo: &dyn Foo = bar;
foo.a();
println!("foo: {}", foo);
Bar::a(bar);
bar.b();
let bar: Arc<dyn Bar> = Arc::new(42);
println!("Arc-bar: {}", bar);
let foo: Arc<dyn Foo> = bar;
println!("Arc-foo: {}", foo);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment