We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 32eecee commit 5fac191Copy full SHA for 5fac191
tests/ui/any/downcast_trait.rs
@@ -0,0 +1,29 @@
1
+//@ run-pass
2
+#![feature(downcast_trait)]
3
+
4
+use std::fmt::Debug;
5
6
+// Look ma, no `T: Debug`
7
+fn downcast_debug_format<T: 'static>(t: &T) -> String {
8
+ match std::any::downcast_trait::<_, dyn Debug>(t) {
9
+ Some(d) => format!("{d:?}"),
10
+ None => "default".to_string()
11
+ }
12
+}
13
14
+// Test that downcasting to a dyn trait works as expected
15
+fn main() {
16
+ #[allow(dead_code)]
17
+ #[derive(Debug)]
18
+ struct A {
19
+ index: usize
20
21
+ let a = A { index: 42 };
22
+ let result = downcast_debug_format(&a);
23
+ assert_eq!("A { index: 42 }", result);
24
25
+ struct B {}
26
+ let b = B {};
27
+ let result = downcast_debug_format(&b);
28
+ assert_eq!("default", result);
29
0 commit comments