Skip to content

Disregard: Broken attempt at type_id hack on stable Rust with no unsafe #139043

Closed as not planned
@traviscross

Description

@traviscross

[Edit: Removed a bunch of things that don't matter because this doesn't do what was intended:]

use core::{any::TypeId, marker::PhantomData};

fn type_id_of<T: ?Sized>() -> TypeId {
    trait Marker {}
    impl<T: ?Sized> Marker for T {}
    fn hack<T: ?Sized + Marker + 'static>(_: *const T) -> TypeId {
        TypeId::of::<T>()
    }
    let p = &PhantomData::<T>;
    let p = &raw const *p as *const (dyn Marker + 'static);
    hack(p)
}

fn type_id_of_val<T: ?Sized>(_: &T) -> TypeId {
    type_id_of::<T>()
}

fn main() {
    let x = ();
    dbg!(type_id_of_val(&x));
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-discussionCategory: Discussion or questions that doesn't represent real issues.T-langRelevant to the language team, which will review and decide on the PR/issue.T-typesRelevant to the types team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions