On 17 Sep 2014, at 23:33, Sean McArthur <smcart...@mozilla.com> wrote:

> On Wed, Sep 17, 2014 at 2:26 PM, Evan Davis <cptr...@gmail.com> wrote:
> 
> The problem is that you're trying to use a trait as a type.
> 
> That shouldn't be a problem. You can use a `&mut Trait`, and you'll get 
> dynamic dispatch.

You cannot mix dynamic dispatch with generic methods.  (This is because we 
implement generic methods by monomorphization, i.e. by creating separate copies 
of the implementation for each instantiation -- but with dynamic dispatch, we 
do not know a priori what all of the calls to the method will be.)

I would link a relevant portion of the Rust manual here, but I cannot actually 
find documentation of this restriction.

----

Pete’s original question was actually answered in his followup email.  It is 
true that you cannot do:

> fn print_numbers(r: &mut Rng) {
>    for _ in range(0u, 10) {
>        println!("{}", r.gen::<uint>());
>    }
> }

due to the restriction I described above, but you can do:

> fn print_numbers<R: Rng> (r: &mut R) {
>    for _ in range(0u, 10) {
>        println!("{}", r.gen::<uint>());
>    }
> }

where the trait is now being used as a bound on a generic type, rather than as 
the basis of an object type.

Cheers,
-Felix

_______________________________________________
Rust-dev mailing list
Rust-dev@mozilla.org
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to