DavidBittner
DavidBittner

Reputation: 535

How can you get the TypeId of the inner value of a Rc or Boxed value, not the TypeId of the enclosing type?

I'm using an associative list to associate TypeIds of event-types and TypeIds of who wants to receive them.

When I attempt to get the TypeId of an Rc<Any>, it gives you the same TypeId (the one of the Rc storing an Any) no matter what the Any is.

#![feature(get_type_id)]

use std::any::*;
use std::rc::Rc;

fn main() {
    let temp: Rc<Any> = Rc::new(13);
    let temp2: &Any = &5;

    assert_eq!(temp.get_type_id(), temp2.get_type_id()); //fails!
}

How can I get the TypeId of the associated Any on the inside?

I believe that Rc<_> implements the Deref trait as well as the Any trait. This means that you can dereference the Rc and call functions in the internal reference. It also means that the Rc has it's own associated TypeId. I'm not looking for the TypeId of the Rc, I'm looking for the TypeId from the dereferenced Any.

Upvotes: 1

Views: 1023

Answers (1)

Shepmaster
Shepmaster

Reputation: 430634

Explicitly dereference the wrapping type:

(*temp).get_type_id()

Upvotes: 6

Related Questions