Hi,

The thing is, I want to use function ptr_eq<T>(@T, @T) from core::managed and I can't change it in the way you describe.

Actually, I'd like to make sure that I can't add twice the same object to a vector like below :

use core::vec::position;
use core::managed::ptr_eq;

struct DummyListener;
trait Listener { fn fire_event(&self); }
impl Listener for DummyListener { fn fire_event(&self) { /* stuff */ } }

fn main() {
    let mut vec: ~[@Listener] = ~[];
    let a = @DummyListener;
    let b = @DummyListener;
    vec = add(vec, a as @Listener);
    vec = add(vec, b as @Listener);
    vec = add(vec, b as @Listener);

   for vec.each |listener| {listener.fire_event();}
}

fn add(mut vec: ~[@Listener], toAdd: @Listener) -> ~[@Listener] {
if ( position(vec, |listener| { ptr_eq::<Listener>(toAdd, *listener) }) == None) {
        vec.push(toAdd);
    }
    vec
}

What do you mean by you can't cast to Traits, isn't it what 'as' is for ?

On 21/05/2013 14:54, Fedor Indutny wrote:
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] <mailto:[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 <http://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] <mailto:[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