Info
This post is auto-generated from RSS feed The Rust Programming Language Forum - Latest topics. Source: Why cant `TypeId` equality be made `const`?
Why cant this function:
fn is_string<T: ?Sized + Any>() -> bool {
TypeId::of::<String>() == TypeId::of::<T>()
}
be made const
? The comparison is implemented in PartialEq::Eq
. The types are known at compile time and const
ing will open up possibilities to write compile time constraints on templates.
I ultimately found a work around (but requires use of nightly intrinsics
feature.)
const fn is_string<T: ?Sized + Any>() -> bool {
core::intrinsics::type_id::<T>() == core::intrinsics::type_id::<String>()
}
The TypeId::t
is a u128
value (comes from core::intrinsics::type_id::<T>()
), so why not make the TypeId::as_u128
public? or better still allow conversion of TypeId::of::<T> as u128
(p.s. this is my 1st week with rust and just getting hang of things so sorry for if it's a noob question)
1 post - 1 participant
🏷️ rust_feed