]>
Commit | Line | Data |
---|---|---|
1a4d82fc JJ |
1 | //! This module implements the `Any` trait, which enables dynamic typing |
2 | //! of any `'static` type through runtime reflection. | |
3 | //! | |
4 | //! `Any` itself can be used to get a `TypeId`, and has more features when used | |
e74abb32 XL |
5 | //! as a trait object. As `&dyn Any` (a borrowed trait object), it has the `is` |
6 | //! and `downcast_ref` methods, to test if the contained value is of a given type, | |
7 | //! and to get a reference to the inner value as a type. As `&mut dyn Any`, there | |
e9174d1e | 8 | //! is also the `downcast_mut` method, for getting a mutable reference to the |
e74abb32 | 9 | //! inner value. `Box<dyn Any>` adds the `downcast` method, which attempts to |
9cc50fc6 | 10 | //! convert to a `Box<T>`. See the [`Box`] documentation for the full details. |
1a4d82fc | 11 | //! |
e74abb32 | 12 | //! Note that `&dyn Any` is limited to testing whether a value is of a specified |
1a4d82fc JJ |
13 | //! concrete type, and cannot be used to test whether a type implements a trait. |
14 | //! | |
54a0048b | 15 | //! [`Box`]: ../../std/boxed/struct.Box.html |
9cc50fc6 | 16 | //! |
5869c6ff XL |
17 | //! # Smart pointers and `dyn Any` |
18 | //! | |
19 | //! One piece of behavior to keep in mind when using `Any` as a trait object, | |
20 | //! especially with types like `Box<dyn Any>` or `Arc<dyn Any>`, is that simply | |
21 | //! calling `.type_id()` on the value will produce the `TypeId` of the | |
22 | //! *container*, not the underlying trait object. This can be avoided by | |
23 | //! converting the smart pointer into a `&dyn Any` instead, which will return | |
24 | //! the object's `TypeId`. For example: | |
25 | //! | |
26 | //! ``` | |
27 | //! use std::any::{Any, TypeId}; | |
28 | //! | |
29 | //! let boxed: Box<dyn Any> = Box::new(3_i32); | |
30 | //! | |
31 | //! // You're more likely to want this: | |
32 | //! let actual_id = (&*boxed).type_id(); | |
33 | //! // ... than this: | |
34 | //! let boxed_id = boxed.type_id(); | |
35 | //! | |
36 | //! assert_eq!(actual_id, TypeId::of::<i32>()); | |
37 | //! assert_eq!(boxed_id, TypeId::of::<Box<dyn Any>>()); | |
38 | //! ``` | |
39 | //! | |
1a4d82fc JJ |
40 | //! # Examples |
41 | //! | |
42 | //! Consider a situation where we want to log out a value passed to a function. | |
85aaf69f | 43 | //! We know the value we're working on implements Debug, but we don't know its |
9fa01778 | 44 | //! concrete type. We want to give special treatment to certain types: in this |
1a4d82fc JJ |
45 | //! case printing out the length of String values prior to their value. |
46 | //! We don't know the concrete type of our value at compile time, so we need to | |
47 | //! use runtime reflection instead. | |
48 | //! | |
49 | //! ```rust | |
85aaf69f | 50 | //! use std::fmt::Debug; |
1a4d82fc JJ |
51 | //! use std::any::Any; |
52 | //! | |
85aaf69f SL |
53 | //! // Logger function for any type that implements Debug. |
54 | //! fn log<T: Any + Debug>(value: &T) { | |
a1dfa0c6 | 55 | //! let value_any = value as &dyn Any; |
1a4d82fc | 56 | //! |
9fa01778 XL |
57 | //! // Try to convert our value to a `String`. If successful, we want to |
58 | //! // output the String`'s length as well as its value. If not, it's a | |
1a4d82fc JJ |
59 | //! // different type: just print it out unadorned. |
60 | //! match value_any.downcast_ref::<String>() { | |
61 | //! Some(as_string) => { | |
62 | //! println!("String ({}): {}", as_string.len(), as_string); | |
63 | //! } | |
64 | //! None => { | |
65 | //! println!("{:?}", value); | |
66 | //! } | |
67 | //! } | |
68 | //! } | |
69 | //! | |
70 | //! // This function wants to log its parameter out prior to doing work with it. | |
c34b1796 | 71 | //! fn do_work<T: Any + Debug>(value: &T) { |
1a4d82fc JJ |
72 | //! log(value); |
73 | //! // ...do some other work | |
74 | //! } | |
75 | //! | |
76 | //! fn main() { | |
77 | //! let my_string = "Hello World".to_string(); | |
78 | //! do_work(&my_string); | |
79 | //! | |
80 | //! let my_i8: i8 = 100; | |
81 | //! do_work(&my_i8); | |
82 | //! } | |
83 | //! ``` | |
84 | ||
85aaf69f | 85 | #![stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc | 86 | |
48663c56 XL |
87 | use crate::fmt; |
88 | use crate::intrinsics; | |
1a4d82fc JJ |
89 | |
90 | /////////////////////////////////////////////////////////////////////////////// | |
91 | // Any trait | |
92 | /////////////////////////////////////////////////////////////////////////////// | |
93 | ||
60c5eb7d | 94 | /// A trait to emulate dynamic typing. |
1a4d82fc | 95 | /// |
3157f602 | 96 | /// Most types implement `Any`. However, any type which contains a non-`'static` reference does not. |
9346a6ac | 97 | /// See the [module-level documentation][mod] for more details. |
c34b1796 | 98 | /// |
3dfed10e | 99 | /// [mod]: crate::any |
dfeec247 XL |
100 | // This trait is not unsafe, though we rely on the specifics of it's sole impl's |
101 | // `type_id` function in unsafe code (e.g., `downcast`). Normally, that would be | |
102 | // a problem, but because the only impl of `Any` is a blanket implementation, no | |
103 | // other code can implement `Any`. | |
104 | // | |
105 | // We could plausibly make this trait unsafe -- it would not cause breakage, | |
106 | // since we control all the implementations -- but we choose not to as that's | |
107 | // both not really necessary and may confuse users about the distinction of | |
108 | // unsafe traits and unsafe methods (i.e., `type_id` would still be safe to call, | |
109 | // but we would likely want to indicate as such in documentation). | |
85aaf69f | 110 | #[stable(feature = "rust1", since = "1.0.0")] |
9e0c209e | 111 | pub trait Any: 'static { |
9346a6ac | 112 | /// Gets the `TypeId` of `self`. |
5bcae85e SL |
113 | /// |
114 | /// # Examples | |
115 | /// | |
116 | /// ``` | |
5bcae85e SL |
117 | /// use std::any::{Any, TypeId}; |
118 | /// | |
a1dfa0c6 | 119 | /// fn is_string(s: &dyn Any) -> bool { |
9fa01778 | 120 | /// TypeId::of::<String>() == s.type_id() |
5bcae85e SL |
121 | /// } |
122 | /// | |
e74abb32 XL |
123 | /// assert_eq!(is_string(&0), false); |
124 | /// assert_eq!(is_string(&"cookie monster".to_string()), true); | |
5bcae85e | 125 | /// ``` |
9fa01778 XL |
126 | #[stable(feature = "get_type_id", since = "1.34.0")] |
127 | fn type_id(&self) -> TypeId; | |
1a4d82fc JJ |
128 | } |
129 | ||
92a42be0 | 130 | #[stable(feature = "rust1", since = "1.0.0")] |
dfeec247 XL |
131 | impl<T: 'static + ?Sized> Any for T { |
132 | fn type_id(&self) -> TypeId { | |
133 | TypeId::of::<T>() | |
134 | } | |
1a4d82fc JJ |
135 | } |
136 | ||
137 | /////////////////////////////////////////////////////////////////////////////// | |
138 | // Extension methods for Any trait objects. | |
1a4d82fc JJ |
139 | /////////////////////////////////////////////////////////////////////////////// |
140 | ||
c34b1796 | 141 | #[stable(feature = "rust1", since = "1.0.0")] |
8faf50e0 | 142 | impl fmt::Debug for dyn Any { |
48663c56 | 143 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
c34b1796 AL |
144 | f.pad("Any") |
145 | } | |
146 | } | |
147 | ||
0731742a | 148 | // Ensure that the result of e.g., joining a thread can be printed and |
9346a6ac AL |
149 | // hence used with `unwrap`. May eventually no longer be needed if |
150 | // dispatch works with upcasting. | |
151 | #[stable(feature = "rust1", since = "1.0.0")] | |
8faf50e0 | 152 | impl fmt::Debug for dyn Any + Send { |
48663c56 | 153 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
9346a6ac AL |
154 | f.pad("Any") |
155 | } | |
156 | } | |
157 | ||
94b46f34 | 158 | #[stable(feature = "any_send_sync_methods", since = "1.28.0")] |
8faf50e0 | 159 | impl fmt::Debug for dyn Any + Send + Sync { |
48663c56 | 160 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
94b46f34 XL |
161 | f.pad("Any") |
162 | } | |
163 | } | |
164 | ||
8faf50e0 | 165 | impl dyn Any { |
cc61c64b | 166 | /// Returns `true` if the boxed type is the same as `T`. |
5bcae85e SL |
167 | /// |
168 | /// # Examples | |
169 | /// | |
170 | /// ``` | |
171 | /// use std::any::Any; | |
172 | /// | |
a1dfa0c6 | 173 | /// fn is_string(s: &dyn Any) { |
5bcae85e SL |
174 | /// if s.is::<String>() { |
175 | /// println!("It's a string!"); | |
176 | /// } else { | |
177 | /// println!("Not a string..."); | |
178 | /// } | |
179 | /// } | |
180 | /// | |
e74abb32 XL |
181 | /// is_string(&0); |
182 | /// is_string(&"cookie monster".to_string()); | |
5bcae85e | 183 | /// ``` |
85aaf69f | 184 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc | 185 | #[inline] |
c34b1796 | 186 | pub fn is<T: Any>(&self) -> bool { |
e1599b0c | 187 | // Get `TypeId` of the type this function is instantiated with. |
1a4d82fc JJ |
188 | let t = TypeId::of::<T>(); |
189 | ||
ba9703b0 | 190 | // Get `TypeId` of the type in the trait object (`self`). |
9fa01778 | 191 | let concrete = self.type_id(); |
1a4d82fc | 192 | |
e1599b0c | 193 | // Compare both `TypeId`s on equality. |
9fa01778 | 194 | t == concrete |
1a4d82fc JJ |
195 | } |
196 | ||
197 | /// Returns some reference to the boxed value if it is of type `T`, or | |
198 | /// `None` if it isn't. | |
5bcae85e SL |
199 | /// |
200 | /// # Examples | |
201 | /// | |
202 | /// ``` | |
203 | /// use std::any::Any; | |
204 | /// | |
a1dfa0c6 | 205 | /// fn print_if_string(s: &dyn Any) { |
5bcae85e SL |
206 | /// if let Some(string) = s.downcast_ref::<String>() { |
207 | /// println!("It's a string({}): '{}'", string.len(), string); | |
208 | /// } else { | |
209 | /// println!("Not a string..."); | |
210 | /// } | |
211 | /// } | |
212 | /// | |
e74abb32 XL |
213 | /// print_if_string(&0); |
214 | /// print_if_string(&"cookie monster".to_string()); | |
5bcae85e | 215 | /// ``` |
85aaf69f | 216 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc | 217 | #[inline] |
c34b1796 | 218 | pub fn downcast_ref<T: Any>(&self) -> Option<&T> { |
1a4d82fc | 219 | if self.is::<T>() { |
74b04a01 XL |
220 | // SAFETY: just checked whether we are pointing to the correct type, and we can rely on |
221 | // that check for memory safety because we have implemented Any for all types; no other | |
222 | // impls can exist as they would conflict with our impl. | |
dfeec247 | 223 | unsafe { Some(&*(self as *const dyn Any as *const T)) } |
1a4d82fc JJ |
224 | } else { |
225 | None | |
226 | } | |
227 | } | |
228 | ||
229 | /// Returns some mutable reference to the boxed value if it is of type `T`, or | |
230 | /// `None` if it isn't. | |
5bcae85e SL |
231 | /// |
232 | /// # Examples | |
233 | /// | |
234 | /// ``` | |
235 | /// use std::any::Any; | |
236 | /// | |
a1dfa0c6 | 237 | /// fn modify_if_u32(s: &mut dyn Any) { |
5bcae85e SL |
238 | /// if let Some(num) = s.downcast_mut::<u32>() { |
239 | /// *num = 42; | |
240 | /// } | |
241 | /// } | |
242 | /// | |
e74abb32 XL |
243 | /// let mut x = 10u32; |
244 | /// let mut s = "starlord".to_string(); | |
5bcae85e | 245 | /// |
e74abb32 XL |
246 | /// modify_if_u32(&mut x); |
247 | /// modify_if_u32(&mut s); | |
5bcae85e | 248 | /// |
e74abb32 XL |
249 | /// assert_eq!(x, 42); |
250 | /// assert_eq!(&s, "starlord"); | |
5bcae85e | 251 | /// ``` |
85aaf69f | 252 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc | 253 | #[inline] |
c34b1796 | 254 | pub fn downcast_mut<T: Any>(&mut self) -> Option<&mut T> { |
1a4d82fc | 255 | if self.is::<T>() { |
74b04a01 XL |
256 | // SAFETY: just checked whether we are pointing to the correct type, and we can rely on |
257 | // that check for memory safety because we have implemented Any for all types; no other | |
258 | // impls can exist as they would conflict with our impl. | |
dfeec247 | 259 | unsafe { Some(&mut *(self as *mut dyn Any as *mut T)) } |
1a4d82fc JJ |
260 | } else { |
261 | None | |
262 | } | |
263 | } | |
264 | } | |
85aaf69f | 265 | |
dfeec247 | 266 | impl dyn Any + Send { |
c34b1796 | 267 | /// Forwards to the method defined on the type `Any`. |
5bcae85e SL |
268 | /// |
269 | /// # Examples | |
270 | /// | |
271 | /// ``` | |
272 | /// use std::any::Any; | |
273 | /// | |
a1dfa0c6 | 274 | /// fn is_string(s: &(dyn Any + Send)) { |
5bcae85e SL |
275 | /// if s.is::<String>() { |
276 | /// println!("It's a string!"); | |
277 | /// } else { | |
278 | /// println!("Not a string..."); | |
279 | /// } | |
280 | /// } | |
281 | /// | |
e74abb32 XL |
282 | /// is_string(&0); |
283 | /// is_string(&"cookie monster".to_string()); | |
5bcae85e | 284 | /// ``` |
c34b1796 AL |
285 | #[stable(feature = "rust1", since = "1.0.0")] |
286 | #[inline] | |
287 | pub fn is<T: Any>(&self) -> bool { | |
6a06907d | 288 | <dyn Any>::is::<T>(self) |
c34b1796 AL |
289 | } |
290 | ||
291 | /// Forwards to the method defined on the type `Any`. | |
5bcae85e SL |
292 | /// |
293 | /// # Examples | |
294 | /// | |
295 | /// ``` | |
296 | /// use std::any::Any; | |
297 | /// | |
a1dfa0c6 | 298 | /// fn print_if_string(s: &(dyn Any + Send)) { |
5bcae85e SL |
299 | /// if let Some(string) = s.downcast_ref::<String>() { |
300 | /// println!("It's a string({}): '{}'", string.len(), string); | |
301 | /// } else { | |
302 | /// println!("Not a string..."); | |
303 | /// } | |
304 | /// } | |
305 | /// | |
e74abb32 XL |
306 | /// print_if_string(&0); |
307 | /// print_if_string(&"cookie monster".to_string()); | |
5bcae85e | 308 | /// ``` |
c34b1796 AL |
309 | #[stable(feature = "rust1", since = "1.0.0")] |
310 | #[inline] | |
311 | pub fn downcast_ref<T: Any>(&self) -> Option<&T> { | |
6a06907d | 312 | <dyn Any>::downcast_ref::<T>(self) |
c34b1796 AL |
313 | } |
314 | ||
315 | /// Forwards to the method defined on the type `Any`. | |
5bcae85e SL |
316 | /// |
317 | /// # Examples | |
318 | /// | |
319 | /// ``` | |
320 | /// use std::any::Any; | |
321 | /// | |
a1dfa0c6 | 322 | /// fn modify_if_u32(s: &mut (dyn Any + Send)) { |
5bcae85e SL |
323 | /// if let Some(num) = s.downcast_mut::<u32>() { |
324 | /// *num = 42; | |
325 | /// } | |
326 | /// } | |
327 | /// | |
e74abb32 XL |
328 | /// let mut x = 10u32; |
329 | /// let mut s = "starlord".to_string(); | |
5bcae85e | 330 | /// |
e74abb32 XL |
331 | /// modify_if_u32(&mut x); |
332 | /// modify_if_u32(&mut s); | |
5bcae85e | 333 | /// |
e74abb32 XL |
334 | /// assert_eq!(x, 42); |
335 | /// assert_eq!(&s, "starlord"); | |
5bcae85e | 336 | /// ``` |
c34b1796 AL |
337 | #[stable(feature = "rust1", since = "1.0.0")] |
338 | #[inline] | |
339 | pub fn downcast_mut<T: Any>(&mut self) -> Option<&mut T> { | |
6a06907d | 340 | <dyn Any>::downcast_mut::<T>(self) |
c34b1796 AL |
341 | } |
342 | } | |
343 | ||
dfeec247 | 344 | impl dyn Any + Send + Sync { |
94b46f34 XL |
345 | /// Forwards to the method defined on the type `Any`. |
346 | /// | |
347 | /// # Examples | |
348 | /// | |
349 | /// ``` | |
350 | /// use std::any::Any; | |
351 | /// | |
a1dfa0c6 | 352 | /// fn is_string(s: &(dyn Any + Send + Sync)) { |
94b46f34 XL |
353 | /// if s.is::<String>() { |
354 | /// println!("It's a string!"); | |
355 | /// } else { | |
356 | /// println!("Not a string..."); | |
357 | /// } | |
358 | /// } | |
359 | /// | |
e74abb32 XL |
360 | /// is_string(&0); |
361 | /// is_string(&"cookie monster".to_string()); | |
94b46f34 XL |
362 | /// ``` |
363 | #[stable(feature = "any_send_sync_methods", since = "1.28.0")] | |
364 | #[inline] | |
365 | pub fn is<T: Any>(&self) -> bool { | |
6a06907d | 366 | <dyn Any>::is::<T>(self) |
94b46f34 XL |
367 | } |
368 | ||
369 | /// Forwards to the method defined on the type `Any`. | |
370 | /// | |
371 | /// # Examples | |
372 | /// | |
373 | /// ``` | |
374 | /// use std::any::Any; | |
375 | /// | |
a1dfa0c6 | 376 | /// fn print_if_string(s: &(dyn Any + Send + Sync)) { |
94b46f34 XL |
377 | /// if let Some(string) = s.downcast_ref::<String>() { |
378 | /// println!("It's a string({}): '{}'", string.len(), string); | |
379 | /// } else { | |
380 | /// println!("Not a string..."); | |
381 | /// } | |
382 | /// } | |
383 | /// | |
e74abb32 XL |
384 | /// print_if_string(&0); |
385 | /// print_if_string(&"cookie monster".to_string()); | |
94b46f34 XL |
386 | /// ``` |
387 | #[stable(feature = "any_send_sync_methods", since = "1.28.0")] | |
388 | #[inline] | |
389 | pub fn downcast_ref<T: Any>(&self) -> Option<&T> { | |
6a06907d | 390 | <dyn Any>::downcast_ref::<T>(self) |
94b46f34 XL |
391 | } |
392 | ||
393 | /// Forwards to the method defined on the type `Any`. | |
394 | /// | |
395 | /// # Examples | |
396 | /// | |
397 | /// ``` | |
398 | /// use std::any::Any; | |
399 | /// | |
a1dfa0c6 | 400 | /// fn modify_if_u32(s: &mut (dyn Any + Send + Sync)) { |
94b46f34 XL |
401 | /// if let Some(num) = s.downcast_mut::<u32>() { |
402 | /// *num = 42; | |
403 | /// } | |
404 | /// } | |
405 | /// | |
e74abb32 XL |
406 | /// let mut x = 10u32; |
407 | /// let mut s = "starlord".to_string(); | |
94b46f34 | 408 | /// |
e74abb32 XL |
409 | /// modify_if_u32(&mut x); |
410 | /// modify_if_u32(&mut s); | |
94b46f34 | 411 | /// |
e74abb32 XL |
412 | /// assert_eq!(x, 42); |
413 | /// assert_eq!(&s, "starlord"); | |
94b46f34 XL |
414 | /// ``` |
415 | #[stable(feature = "any_send_sync_methods", since = "1.28.0")] | |
416 | #[inline] | |
417 | pub fn downcast_mut<T: Any>(&mut self) -> Option<&mut T> { | |
6a06907d | 418 | <dyn Any>::downcast_mut::<T>(self) |
94b46f34 XL |
419 | } |
420 | } | |
c34b1796 | 421 | |
85aaf69f SL |
422 | /////////////////////////////////////////////////////////////////////////////// |
423 | // TypeID and its methods | |
424 | /////////////////////////////////////////////////////////////////////////////// | |
425 | ||
426 | /// A `TypeId` represents a globally unique identifier for a type. | |
427 | /// | |
428 | /// Each `TypeId` is an opaque object which does not allow inspection of what's | |
429 | /// inside but does allow basic operations such as cloning, comparison, | |
430 | /// printing, and showing. | |
431 | /// | |
432 | /// A `TypeId` is currently only available for types which ascribe to `'static`, | |
433 | /// but this limitation may be removed in the future. | |
8bb4bdeb XL |
434 | /// |
435 | /// While `TypeId` implements `Hash`, `PartialOrd`, and `Ord`, it is worth | |
436 | /// noting that the hashes and ordering will vary between Rust releases. Beware | |
8faf50e0 | 437 | /// of relying on them inside of your code! |
8bb4bdeb | 438 | #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)] |
85aaf69f SL |
439 | #[stable(feature = "rust1", since = "1.0.0")] |
440 | pub struct TypeId { | |
441 | t: u64, | |
442 | } | |
443 | ||
444 | impl TypeId { | |
2c00a5a8 XL |
445 | /// Returns the `TypeId` of the type this generic function has been |
446 | /// instantiated with. | |
447 | /// | |
448 | /// # Examples | |
449 | /// | |
450 | /// ``` | |
451 | /// use std::any::{Any, TypeId}; | |
452 | /// | |
453 | /// fn is_string<T: ?Sized + Any>(_s: &T) -> bool { | |
454 | /// TypeId::of::<String>() == TypeId::of::<T>() | |
455 | /// } | |
456 | /// | |
e74abb32 XL |
457 | /// assert_eq!(is_string(&0), false); |
458 | /// assert_eq!(is_string(&"cookie monster".to_string()), true); | |
2c00a5a8 XL |
459 | /// ``` |
460 | #[stable(feature = "rust1", since = "1.0.0")] | |
6c58768f | 461 | #[rustc_const_unstable(feature = "const_type_id", issue = "77125")] |
2c00a5a8 | 462 | pub const fn of<T: ?Sized + 'static>() -> TypeId { |
dfeec247 | 463 | TypeId { t: intrinsics::type_id::<T>() } |
2c00a5a8 | 464 | } |
85aaf69f | 465 | } |
416331ca XL |
466 | |
467 | /// Returns the name of a type as a string slice. | |
468 | /// | |
469 | /// # Note | |
470 | /// | |
471 | /// This is intended for diagnostic use. The exact contents and format of the | |
f9f354fc XL |
472 | /// string returned are not specified, other than being a best-effort |
473 | /// description of the type. For example, amongst the strings | |
474 | /// that `type_name::<Option<String>>()` might return are `"Option<String>"` and | |
475 | /// `"std::option::Option<std::string::String>"`. | |
416331ca | 476 | /// |
f9f354fc XL |
477 | /// The returned string must not be considered to be a unique identifier of a |
478 | /// type as multiple types may map to the same type name. Similarly, there is no | |
479 | /// guarantee that all parts of a type will appear in the returned string: for | |
480 | /// example, lifetime specifiers are currently not included. In addition, the | |
481 | /// output may change between versions of the compiler. | |
416331ca XL |
482 | /// |
483 | /// The current implementation uses the same infrastructure as compiler | |
484 | /// diagnostics and debuginfo, but this is not guaranteed. | |
e74abb32 | 485 | /// |
60c5eb7d | 486 | /// # Examples |
e74abb32 XL |
487 | /// |
488 | /// ```rust | |
489 | /// assert_eq!( | |
490 | /// std::any::type_name::<Option<String>>(), | |
491 | /// "core::option::Option<alloc::string::String>", | |
492 | /// ); | |
493 | /// ``` | |
416331ca | 494 | #[stable(feature = "type_name", since = "1.38.0")] |
dfeec247 | 495 | #[rustc_const_unstable(feature = "const_type_name", issue = "63084")] |
416331ca | 496 | pub const fn type_name<T: ?Sized>() -> &'static str { |
416331ca XL |
497 | intrinsics::type_name::<T>() |
498 | } | |
60c5eb7d XL |
499 | |
500 | /// Returns the name of the type of the pointed-to value as a string slice. | |
501 | /// This is the same as `type_name::<T>()`, but can be used where the type of a | |
502 | /// variable is not easily available. | |
503 | /// | |
504 | /// # Note | |
505 | /// | |
506 | /// This is intended for diagnostic use. The exact contents and format of the | |
507 | /// string are not specified, other than being a best-effort description of the | |
dfeec247 | 508 | /// type. For example, `type_name_of_val::<Option<String>>(None)` could return |
60c5eb7d XL |
509 | /// `"Option<String>"` or `"std::option::Option<std::string::String>"`, but not |
510 | /// `"foobar"`. In addition, the output may change between versions of the | |
511 | /// compiler. | |
512 | /// | |
dfeec247 XL |
513 | /// This function does not resolve trait objects, |
514 | /// meaning that `type_name_of_val(&7u32 as &dyn Debug)` | |
515 | /// may return `"dyn Debug"`, but not `"u32"`. | |
516 | /// | |
60c5eb7d XL |
517 | /// The type name should not be considered a unique identifier of a type; |
518 | /// multiple types may share the same type name. | |
519 | /// | |
520 | /// The current implementation uses the same infrastructure as compiler | |
521 | /// diagnostics and debuginfo, but this is not guaranteed. | |
522 | /// | |
523 | /// # Examples | |
524 | /// | |
525 | /// Prints the default integer and float types. | |
526 | /// | |
527 | /// ```rust | |
528 | /// #![feature(type_name_of_val)] | |
529 | /// use std::any::type_name_of_val; | |
530 | /// | |
531 | /// let x = 1; | |
532 | /// println!("{}", type_name_of_val(&x)); | |
533 | /// let y = 1.0; | |
534 | /// println!("{}", type_name_of_val(&y)); | |
535 | /// ``` | |
536 | #[unstable(feature = "type_name_of_val", issue = "66359")] | |
dfeec247 XL |
537 | #[rustc_const_unstable(feature = "const_type_name", issue = "63084")] |
538 | pub const fn type_name_of_val<T: ?Sized>(_val: &T) -> &'static str { | |
60c5eb7d XL |
539 | type_name::<T>() |
540 | } |