Skip to content

ACP: add std::any::is_same_type::<T, U>() -> bool #411

Closed as not planned
Closed as not planned
@tgross35

Description

@tgross35

Proposal

Problem statement

#231 was recently accepted as a way to check if two types are the same in const contexts, providing a new TypeId::matches. However, this API suffers from a few problems:

  1. Its goal is to be useful at compile time, but it relies on being able to construct TypeIds. Whether or not we can provide a safe const TypeId::of is not well known at this time; if it is possible, it will likely be a while off. (Tracking Issue for const fn type_id rust#77125, Collisions in type_id rust#10389)
  2. If we eventually get a const PartialEq, TypeId::matches will become a duplicate of a more natural API.

These two problems seem to provide significant barriers to eventual stabiliztion of TypeId::matches

Motivating examples or use cases

Most of the use cases for const type equality checks is comparing types that are available either as a generic or as a concrete type, rather than unknown TypeIds. This is currently true by default since const TypeId::of is not available, but seems likely to be the case more generally.

One use case of checking types for equality is to provide downcasting behavior:

use core::ptr;

const fn const_downcast<T, Expected>(value: &T) -> Option<&Expected> {
    if /* magic expression */ {
        Some( unsafe { &*ptr::from_ref(value).cast::<Expected>() })
    } else {
        None
    }
}

#231 provides a more complete example.

Solution sketch

Rather than provide an interface that relies on TypeIds, provide a standalone function that takes the types as generic parameters:

// core::any

/// Return `true` if `T` and `U` are the same type.
const fn is_same_type<T: 'static, U: 'static>() -> bool {
    // or intrinsic call
    TypeId::of::<T>().t == TypeId::of::<U>().t
}

Usage:

use core::{any, ptr};

const fn const_downcast<T, Expected>(value: &T) -> Option<&Expected> {
    if any::is_same_type::<T, Expected>() {
        Some( unsafe { &*ptr::from_ref(value).cast::<Expected>() })
    } else {
        None
    }
}

This can be implemented using TypeId (as in this example), but it could also be implemented as an intrinsic. The intrinsic option allows hooking into compiler logic to provide the check, meaning const stability is not blocked on const TypeId uncertainties.

Usage is also simpler, which means it serves a simplification purpose even after const PartialEq.

Alternatives

Links and related work

What happens now?

This issue contains an API change proposal (or ACP) and is part of the libs-api team feature lifecycle. Once this issue is filed, the libs-api team will review open proposals as capability becomes available. Current response times do not have a clear estimate, but may be up to several months.

Possible responses

The libs team may respond in various different ways. First, the team will consider the problem (this doesn't require any concrete solution or alternatives to have been proposed):

  • We think this problem seems worth solving, and the standard library might be the right place to solve it.
  • We think that this probably doesn't belong in the standard library.

Second, if there's a concrete solution:

  • We think this specific solution looks roughly right, approved, you or someone else should implement this. (Further review will still happen on the subsequent implementation PR.)
  • We're not sure this is the right solution, and the alternatives or other materials don't give us enough information to be sure about that. Here are some questions we have that aren't answered, or rough ideas about alternatives we'd want to see discussed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    T-libs-apiapi-change-proposalA proposal to add or alter unstable APIs in the standard libraries

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions