-
Notifications
You must be signed in to change notification settings - Fork 12.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Tracking issue for any::type_name_of_val
#66359
Comments
This comment has been minimized.
This comment has been minimized.
It might be worth considering adding a note about the usage of
Some users might get confused as to why |
improve type_name_of_val docs suggested by @Globidev in rust-lang#66359 (comment)
improve type_name_of_val docs suggested by @Globidev in rust-lang#66359 (comment)
used in a question on reddit |
Would something like |
One big issue of #![feature(type_name_of_val)]
fn main() {
let x: u32 = 3;
let y: &dyn Send = &x;
println!("{}", std::any::type_name_of_val(y)); // prints something like 'dyn Send'
} I think |
Perhaps Or are we saying that this should be changed to get type name form dynamic objects at runtime? |
getting the type name at runtime isn't really possible without having a non zero cost even for programs never using
|
Perhaps |
Just tried to use this for what it says on the tin. It wasn't able to work for my situation. I'm trying to print the type name of a partially-moved struct. type_name_of_val(&expr);
Does this need to be compiler magic to let this work? |
@mehcode |
@mehcode you can write the following helper to let you query the type name of an unusable binding: macro_rules! type_name_of {( $e:expr $(,)? ) => ({
let it = [];
#[allow(unreachable_code)] {
if false {
loop {} // disables borrowck and dropck
(&mut { it })[0] = &$e; // nudges type inference
}
}
$crate::__helper__(it)
})} pub(in crate) use type_name_of;
#[doc(hidden)] pub
fn __helper__<T> (_: [&T; 0])
-> &'static str
{
::core::any::type_name::<T>()
} With it, the following snippet compiles and runs fine: let it = (vec![()], 42);
drop(it.0);
dbg!(type_name_of!(it)); |
Is the only blocker here naming? It seems like I don't think that there is much we would want to do for arbitrary trait AnyNamed {
fn name(&self) -> &'static str {
std::any::type_name::<Self>()
}
}
impl<T: ?Sized> AnyNamed for T {}
fn type_name_dyn(val: &dyn AnyNamed) {
println!("{}", val.name());
}
fn main() {
type_name_dyn(&"abcd".to_owned()); // alloc::string::String
type_name_dyn(&100u8); // u8
type_name_dyn(&10.0f32); // f32
} Or, if you only need a few types you can just check if your macro_rules! dyn_impl {
($val:ident, $($ty:ty),*) => { $(
if $val.is::<$ty>() {
stringify!($ty)
} else )* {
"unknown"
}}
}
fn type_name_dyn(val: &dyn std::any::Any) {
let name = dyn_impl!(val, String, u8, f32);
println!("{name}");
}
fn main() {
type_name_dyn(&"abcd".to_owned()); // String
type_name_dyn(&100u8); // u8
type_name_dyn(&10.0f32); // f32
type_name_dyn(&"abcd"); // unknown
} |
Make the following API stable: // in core::any pub fn type_name_of_val<T: ?Sized>(_val: &T) -> &'static str Const stability is not added because this relies on `type_name` which is also not const. That has a blocking issue. Fixes rust-lang#66359
I created a stabilization PR at #118234 |
@rust-lang/libs-api: https://doc.rust-lang.org/nightly/core/any/fn.type_name_of_val.html Context: this function is intended to be useful when you have a value whose type cannot be named in Rust code, so directly calling fn main() {
println!("{}", type_name_of_val(&main)); // "playground::main"
f(false);
}
fn f(asdf: impl Display) {
println!("{}", type_name_of_val(&asdf)); // "bool"
} Stabilizing fn type_name_of_val<T: ?Sized>(_: &T) -> &'static str {
type_name::<T>()
} Notes from @tgross35 in #118234 (comment):
|
Team member @dtolnay has proposed to merge this. The next step is review by the rest of the tagged team members: No concerns currently listed. Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! See this document for info about what commands tagged team members can give me. |
Make the following API stable: // in core::any pub fn type_name_of_val<T: ?Sized>(_val: &T) -> &'static str Const stability is not added because this relies on `type_name` which is also not const. That has a blocking issue. Fixes rust-lang#66359
🔔 This is now entering its final comment period, as per the review above. 🔔 |
The final comment period, with a disposition to merge, as per the review above, is now complete. As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed. This will be merged soon. |
…lnay Stabilize `type_name_of_val` Make the following API stable: ```rust // in core::any pub fn type_name_of_val<T: ?Sized>(_val: &T) -> &'static str ``` This is a convenience method to get the type name of a value, as opposed to `type_name` that takes a type as a generic. Const stability is not added because this relies on `type_name` which is also not const. That has a blocking issue rust-lang#97156. Wording was also changed to direct most of the details to `type_name` so we don't have as much duplicated documentation. Fixes tracking issue rust-lang#66359. There were two main concerns in the tracking issue: 1. Naming: `type_name_of` and `type_name_of_val` seem like the only mentioned options. Differences in opinion here come from `std::mem::{size_of, align_of, size_of_val, align_of_val}`. This PR leaves the name as `type_name_of_val`, but I can change if desired since it is pretty verbose. 2. What this displays for `&dyn`: I don't think that having `type_name_of_val` function resolve those is worth the headache it would be, see rust-lang#66359 (comment) for some workarounds. I also amended the docs wording to leave it open-ended, in case we have means to change that behavior in the future. `@rustbot` label -T-libs +T-libs-api +needs-fcp r? libs-api
…lnay Stabilize `type_name_of_val` Make the following API stable: ```rust // in core::any pub fn type_name_of_val<T: ?Sized>(_val: &T) -> &'static str ``` This is a convenience method to get the type name of a value, as opposed to `type_name` that takes a type as a generic. Const stability is not added because this relies on `type_name` which is also not const. That has a blocking issue rust-lang#97156. Wording was also changed to direct most of the details to `type_name` so we don't have as much duplicated documentation. Fixes tracking issue rust-lang#66359. There were two main concerns in the tracking issue: 1. Naming: `type_name_of` and `type_name_of_val` seem like the only mentioned options. Differences in opinion here come from `std::mem::{size_of, align_of, size_of_val, align_of_val}`. This PR leaves the name as `type_name_of_val`, but I can change if desired since it is pretty verbose. 2. What this displays for `&dyn`: I don't think that having `type_name_of_val` function resolve those is worth the headache it would be, see rust-lang#66359 (comment) for some workarounds. I also amended the docs wording to leave it open-ended, in case we have means to change that behavior in the future. ``@rustbot`` label -T-libs +T-libs-api +needs-fcp r? libs-api
Rollup merge of rust-lang#118234 - tgross35:type_name_of_value, r=dtolnay Stabilize `type_name_of_val` Make the following API stable: ```rust // in core::any pub fn type_name_of_val<T: ?Sized>(_val: &T) -> &'static str ``` This is a convenience method to get the type name of a value, as opposed to `type_name` that takes a type as a generic. Const stability is not added because this relies on `type_name` which is also not const. That has a blocking issue rust-lang#97156. Wording was also changed to direct most of the details to `type_name` so we don't have as much duplicated documentation. Fixes tracking issue rust-lang#66359. There were two main concerns in the tracking issue: 1. Naming: `type_name_of` and `type_name_of_val` seem like the only mentioned options. Differences in opinion here come from `std::mem::{size_of, align_of, size_of_val, align_of_val}`. This PR leaves the name as `type_name_of_val`, but I can change if desired since it is pretty verbose. 2. What this displays for `&dyn`: I don't think that having `type_name_of_val` function resolve those is worth the headache it would be, see rust-lang#66359 (comment) for some workarounds. I also amended the docs wording to leave it open-ended, in case we have means to change that behavior in the future. ``@rustbot`` label -T-libs +T-libs-api +needs-fcp r? libs-api
#65961 added the function
core::any::type_name_of_val
.Unresolved issues:
type_name_of_val(&val)
is quite verbose,type_name_of(&val)
might be better, even ifalign_of
,align_of_val
andsize_of
andsize_of_val
already exist.The text was updated successfully, but these errors were encountered: