What does `impl TraitX for TraitY` mean in Rust? -
for example:
trait traitx { } trait traity { } impl traitx traity { }
i figured mean same as
impl<a: traity> traitx { }
but error message suggests otherwise:
$ rustc --version rustc 0.12.0-nightly (a70a0374e 2014-10-01 21:27:19 +0000) $ rustc test.rs test.rs:3:17: 3:23 error: explicit lifetime bound required test.rs:3 impl traitx traity { } ^~~~~~
does impl traitx traity
(or variant of explicit lifetime) mean in rust? if so, illustration of use?
impl traitx traity
using traity
a dynamically sized type (dst). if add together required lifetime bound (see e.g. this more info necessity of lifetime bound), compiler complain in manner:
trait traitx { } trait traity { } impl<'a> traitx traity+'a { } fn main() {}
<anon>:3:1: 3:34 error: trait `core::kinds::sized` not implemented type `traity+'a` <anon>:3 impl<'a> traitx traity+'a { } ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ <anon>:3:1: 3:34 note: trait `core::kinds::sized` must implemented because required `traitx` <anon>:3 impl<'a> traitx traity+'a { } ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
playpen
the errors saying traity+'a
not sized, is, doesn't have size known @ compile time (e.g. u8
has size 1, vec<t>
size of 3 pointers).
the syntax implementing traitx
traity
trait objects (these covered in "object types" section of reference), allowing handled (behind pointer) in places value implementing traitx
expected. working usage involves sized?
annotations, these whatever they're attached optionally (?
) sized (the default things assumed sized).
#![allow(dead_code)] // indicate it's ok implement trait dsts trait traitx sized? { } trait traity { } trait traitz { } impl<'a> traitx traity+'a { } // sized? allow dsts passed this. fn example<sized? t: traitx>(_: &t) {} fn call_it(x: &traity, _y: &traitz) { example::<traity>(x); // possible if `traity` impls `traitx`. // error: // example::<traitz>(_y); // `traitz` doesn't impl `traitx`. } fn main() {}
playpen
the explicit ::<traity>
type hint required when calling function unsized type now, bug #17178. moment, there's still quite few bugs dst it's not easy utilize in practice, improve.
the major motivation dst making handling trait objects more consistent other pointer types, e.g. back upwards &trait
, box<trait>
trait objects, dst designed allow other pointer types rc<trait>
, arc<trait>
. dst allows treating real pointers, e.g. if obj: box<trait>
&*obj
possible dst, illegal because trait objects fat pointers, not normal pointers.
rust
No comments:
Post a Comment