gpt4 book ai didi

rust - 在Rust中,为什么 `std::any::TypeId::of`不是 `const fn`?

转载 作者:行者123 更新时间:2023-12-03 11:41:27 25 4
gpt4 key购买 nike

一个明显的问题:
为什么std::any::TypeId::of::<T>不是const fn
由于std::any::type_name::<T>已经是const fn,我找不到任何原因。

最佳答案

如果查看the code(单击its documentation右侧的[src]按钮),您会看到of标记为const,但是上面有rustc_const_unstable属性:

#[rustc_const_unstable(feature = "const_type_id", issue = "77125")]
这告诉我们,至少稳定地使 TypeId::of成为 const的跟踪问题是 #77125。在该期中,有一个指向 PR that reverted its initial stabilization的链接,进一步链接到一个 comment by eddyb:

TypeId is one a few types (mem::Discriminant is the only other one I can think of) that's a thin wrapper for a private integer, at least right now. This is ripe for abuse no matter what we say/document it as.

I'm not saying we should cater to this, but rather avoid stabilizing even worse abuse-enabling tools like const fn TypeId::of.Like I said, I am already aware of usecases where compile-time transmute(TypeId::of::<T>()) is an uniquely useful tool and someone might not think twice before releasing a crate which uses it somewhere internally and becomes widely-used.


因此,这里的主要问题似乎是不道德的代码滥用 TypeId::of来在编译时执行操作的可能性,这种方式使得将来更改 TypeId的实现很难在没有重大破损的情况下进行。

关于rust - 在Rust中,为什么 `std::any::TypeId::of`不是 `const fn`?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66493080/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com