Hi,
I am not the OP, but this question made me curious.
If I understand the problem correctly, the type system makes a strong
distinction between an @Struct and an @Trait. And ptr_eq seems to accept
@Struct only.
How can this restriction be inferred from the signature of ptr_eq ? Is this
because it's ptr_eq<T> instead of ptr_eq<@T> ? Does the fact that this function
is marked as inlined has an influence on that behavior ?
I would be interrested in the answer of the original question, too (how to
compare 2 @Trait pointers).
Thanks.
----- Message d'origine -----
De : Vincent Ocquet
Envoyés : 21.05.13 20:15
À : Fedor Indutny
Objet : Re: [rust-dev] Boxed traits and generics.
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] >
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)
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]
https://mail.mozilla.org/listinfo/rust-dev
_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev