Skip to content

Add a TypeID intrinsic #9913

Closed
Closed
@Kimundi

Description

@Kimundi

It's needed for things like an Any type, or safe dynamic loading.

It basically needs to be an generic intrinsic that returns some kind of data structure that uniquely identifies the generic type it has been instantiated with. Ideally it also includes information about the crate itself and it's meta data, so that two different named and/or versioned crates with the same type result in different TypeIDs.

Currently, the static pointers to type descriptors can be kinda used for this, but there is no guarantee that a type descriptor can not be duplicated, so using them for this purpose is incorrect.

Possible usage could look like this:

fn same_types<T, U>() -> bool {
    let type_id_t = intrinsics::type_id::<T>();
    let type_id_u = intrinsics::type_id::<U>();

    type_id_t == type_id_u
}

fn main() {
    assert!(same_types::<uint, uint>());

    let type_id = intrinsics::type_id::<std::util::Void>();
    assert!(type_id.path().to_str() == "<6df45e453>::std::util::Void");
    assert!(type_id.crate().name() == "std");
    assert!(type_id.crate().version() == "1.0");
    assert!(type_id.crate().hash() == "6df45e453");

}

See also https://gist.github.com/Kimundi/6802198 for some prototype implementation of a TypeID using type descriptors.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions