Skip to content

Instantly share code, notes, and snippets.

@vi
Last active January 3, 2016 07:39
Show Gist options
  • Select an option

  • Save vi/8430486 to your computer and use it in GitHub Desktop.

Select an option

Save vi/8430486 to your computer and use it in GitHub Desktop.
Attempt to play with trait ingeritence in Rust
trait Drawable { fn draw(&self); }
trait VeryDrawable : Drawable + Clone { fn draw_twice(&self) { self.draw(); self.draw(); } }
#[deriving(Eq, Clone)]
enum Circle { Circle }
#[deriving(Eq, Clone)]
struct Rectangle;
impl Drawable for Circle { fn draw(&self) { println("C"); } }
impl Drawable for Rectangle { fn draw(&self) { println("R"); } }
impl VeryDrawable for Circle { }.
impl VeryDrawable for Rectangle {}.
struct ConvertVeryDrawableToDrawable { x : ~VeryDrawable }
impl Drawable for ConvertVeryDrawableToDrawable { fn draw(&self) { self.x.draw() } }
fn draw_all(shapes: &[&Drawable]) {
for shape in shapes.iter() { shape.draw(); }
}
fn main() -> () {
let a : [&VeryDrawable, .. 2] = [ &Circle as &VeryDrawable , &Rectangle as &VeryDrawable];
let b : &[&VeryDrawable] = a;
let c : &[&Drawable] = b.map(|&i| &ConvertVeryDrawableToDrawable { x: ~ (i.clone())} as &Drawable);
draw_all(c);
}
/*
tut.rs:33:78: 33:88 error: cannot call a method whose type contains a self-type through an object
tut.rs:33 let c : &[&Drawable] = b.map(|&i| &ConvertVeryDrawableToDrawable { x: ~ (i.clone())} as &Drawable);
^~~~~~~~~~
*/
trait Drawable { fn draw(&self); }
trait VeryDrawable : Drawable + Send { fn draw_twice(&self) { self.draw(); self.draw(); } }
#[deriving(Eq, Clone)]
enum Circle { Circle }
#[deriving(Eq, Clone)]
struct Rectangle;
impl Drawable for Circle { fn draw(&self) { println("C"); } }
impl Drawable for Rectangle { fn draw(&self) { println("R"); } }
impl VeryDrawable for Circle { }.
impl VeryDrawable for Rectangle {}.
struct ConvertVeryDrawableToDrawable<'a> { x : &'a VeryDrawable }
impl<'a> Drawable for ConvertVeryDrawableToDrawable<'a> { fn draw(&self) { self.x.draw() } }
//fn draw_all(shapes: &[&Drawable]) {
// for shape in shapes.iter() { shape.draw(); }
//}
fn draw_all(shapes: &[&VeryDrawable]) {
for shape in shapes.iter() { shape.draw(); }
}
fn main() -> () {
let a : [&VeryDrawable, .. 2] = [ &Circle as &VeryDrawable , &Rectangle as &VeryDrawable];
let b : &[&VeryDrawable] = a;
let c : &[~Drawable] = b.map(|&i| ~ConvertVeryDrawableToDrawable { x: i} as ~Drawable);
draw_all(b);
}
/*
tut.rs:36:39: 36:69 error: cannot pack type `~ConvertVeryDrawableToDrawable<>`, which does not fulfill `Send`, as a trait bounded by Send
tut.rs:36 let c : &[~Drawable] = b.map(|&i| ~ConvertVeryDrawableToDrawable { x: i} as ~Drawable);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
trait Drawable { fn draw(&self); }
trait VeryDrawable : Drawable + Clone { fn draw_twice(&self) { self.draw(); self.draw(); } }
#[deriving(Eq, Clone)]
enum Circle { Circle }
#[deriving(Eq, Clone)]
struct Rectangle;
impl Drawable for Circle { fn draw(&self) { println("C"); } }
impl Drawable for Rectangle { fn draw(&self) { println("R"); } }
impl VeryDrawable for Circle { }.
impl VeryDrawable for Rectangle {}.
struct ConvertVeryDrawableToDrawable<'a> { x : &'a VeryDrawable }
impl<'a> Drawable for ConvertVeryDrawableToDrawable<'a> { fn draw(&self) { self.x.draw() } }
//fn draw_all(shapes: &[&Drawable]) {
// for shape in shapes.iter() { shape.draw(); }
//}
fn draw_all(shapes: &[&VeryDrawable]) {
for shape in shapes.iter() { shape.draw(); }
}
fn main() -> () {
let a : [&VeryDrawable, .. 2] = [ &Circle as &VeryDrawable , &Rectangle as &VeryDrawable];
let b : &[&VeryDrawable] = a;
let c : &[&Drawable] = b.map(|&i| &ConvertVeryDrawableToDrawable { x: i} as &Drawable);
draw_all(b);
}
/*
tut.rs:36:39: 36:69 error: borrowed value does not live long enough
tut.rs:36 let c : &[&Drawable] = b.map(|&i| &ConvertVeryDrawableToDrawable { x: i} as &Drawable);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
tut.rs:28:17: 38:2 note: reference must be valid for the block at 28:16...
tut.rs:28 fn main() -> () {
tut.rs:29 let w = Cell::new(3);
tut.rs:30 let q = ~||w.get();
tut.rs:31 w.set(4);
tut.rs:32 println(format!("{} {}", (*q)(), w.get()));
tut.rs:33
...
tut.rs:36:39: 36:90 note: ...but borrowed value is only valid for the block at 36:38
tut.rs:36 let c : &[&Drawable] = b.map(|&i| &ConvertVeryDrawableToDrawable { x: i} as &Drawable);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
@kvark
Copy link

kvark commented Jan 15, 2014

I don't think it's going to work like that at all. An a array of references is something that hard to work with because it's hard for Rust to ensure the memory safety of those, so it puts a lot of limitations on you (i.e. I'd be surprised if you can reference each element with map() in a way that you do).
Again, real devs may provide more insight on that case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment