Skip to content

Instantly share code, notes, and snippets.

@glaebhoerl
Last active August 29, 2015 14:04
Show Gist options
  • Save glaebhoerl/57d50d57be6579c00849 to your computer and use it in GitHub Desktop.
Save glaebhoerl/57d50d57be6579c00849 to your computer and use it in GitHub Desktop.
trait ToInt {
fn to_int(&self) -> int { 666 }
}
struct A<'a> {
a: &'a Box<int>
}
impl<'a> ToInt for A<'a> {
fn to_int(&self) -> int { **self.a }
}
struct B<'b> {
b: fn(&'b Box<int>) -> int
}
impl<'b> ToInt for B<'b> { }
struct C<'c> {
a: &'c Box<int>,
b: fn(&'c Box<int>) -> int
}
impl<'c> ToInt for C<'c> {
fn to_int(&self) -> int { (self.b)(self.a) }
}
struct D<'d>;
impl<'d> ToInt for D<'d> { }
fn foo(foo: Box<ToInt + 'static>) { println!("foo: {}", foo.to_int()) }
fn main() {
let a = &box 42;
fn b(b: &Box<int>) -> int { **b * 10 }
foo(box A { a: a }); // error
foo(box B { b: b });
foo(box C { a: a, b: b }); // error
foo(box D)
}
/*
error: cannot pack type `Box<A<'_>>`, which does not fulfill `'static`, as a trait bounded by 'static
error: cannot pack type `Box<C<'_>>`, which does not fulfill `'static`, as a trait bounded by 'static
error: aborting due to 2 previous errors
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment