Skip to content

Commit 46224d5

Browse files
committed
Add downcast_trait ui tests
1 parent 7a46b59 commit 46224d5

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

tests/ui/any/downcast_trait.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)