Hi!

You can't cast structs to traits, traits is just a behaviour for structs.
What you probably wanted to do was:

fn foo<T: ToStr>(value: @T) -> ~str { value.to_str() }

Cheers,
Fedor.


On Tue, May 21, 2013 at 3:50 PM, Vincent O. <[email protected]>wrote:

> Hi,
>
> I tried to use core::managed::ptr_eq for boxed traits but it compile-fails
> with a mismatched type error.
>
> Here's a code example that reproduces my problem :
>
> use core::ToStr;
>
> fn main() {
>     let a = @S;
>     foo::<ToStr>(a as @ToStr);
> }
>
> struct S;
> impl S for ToStr { fn to-str(&self) -> ~str {~""} }
> fn foo<T>(value: @T) {} // similar to ptr_eq
>
> with the error message :
> test.rs:5:15: 5:26 error: mismatched types: expected
> `@core::to_str::ToStr` but found `@core::to_str::ToStr` (expected @-ptr but
> found trait core::to_str::ToStr)
> test.rs:5   test::<ToStr>(a as @ToStr);
>
> The issue is the same with owned boxes. Is it the intended behavior ? in
> this case, is there any workaround ?
>
> _______________________________________________
> Rust-dev mailing list
> [email protected]
> https://mail.mozilla.org/listinfo/rust-dev
>
>
_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to