Skip to content

Commit ea48ffc

Browse files
ivarflakstadizagawd
authored andcommitted
Add downcast_trait_mut ui tests
1 parent 5fac191 commit ea48ffc

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

tests/ui/any/downcast_trait_mut.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//@ run-pass
2+
#![feature(downcast_trait)]
3+
4+
use std::fmt::{Error, Write};
5+
6+
// Look ma, no `T: Write`
7+
fn downcast_mut_write<T: 'static>(t: &mut T, s: &str) -> Result<(), Error> {
8+
match std::any::downcast_trait_mut::<_, dyn Write>(t) {
9+
Some(w) => w.write_str(s),
10+
None => Ok(())
11+
}
12+
}
13+
14+
// Test that downcasting to a mut dyn trait works as expected
15+
fn main() {
16+
let mut buf = "Hello".to_string();
17+
18+
downcast_mut_write(&mut buf, " world!").unwrap();
19+
assert_eq!(buf, "Hello world!");
20+
}

0 commit comments

Comments
 (0)