Skip to content

Comprison of TypeIds in const context #73900

Open
@WaffleLapkin

Description

@WaffleLapkin

Is there any way to compare 2 TypeIds in const context? (It's possible to create them in const context, and it's even soon to be stabilized)

What I want is a static check for type (un)equality without auto traits:

// will fail with index out of bounds if T == U
const _: () = [()][(TypeId::of::<T>() == TypeId::of::<U>()) as usize];

(this exact code currently fails to compile with "calls in constants are limited to constant functions, tuple structs and tuple variants" error, because == isn't const)

Maybe it's a good idea to add API like the following to TypeId?

impl TypeId {
    const fn eq(&self, rhs: &Self) -> bool {
        self.t == rhs.t
    }
}

I could work on this, though I haven't seen similar apps in std, so I wanted to ask about it before doing anything.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)S-blockedStatus: Blocked on something else such as an RFC or other implementation work.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions