I can create a Write
trait object that points to a (heap-stored) File
object as such:
let a : Box<dyn Write> = Box::new(File::open("/dev/null").unwrap()); // Compiles fine.
Using the turbofish notation, however, produces an error (Rust 1.56.1):
let a : Box<dyn Write> = Box::<dyn Write>::new(File::open("/dev/null").unwrap()); // Fails to compile.
The error being:
error[E0599]: the function or associated item `new` exists for struct `Box<dyn std::io::Write>`, but its trait bounds were not satisfied
--> src/main.rs:37:48
|
37 | let a : Box<dyn Write> = Box::<dyn Write>::new(File::open("/dev/null").unwrap()); // Fails to compile.
| ^^^ function or associated item cannot be called on `Box<dyn std::io::Write>` due to unsatisfied trait bounds
|
::: /home/[REDACTED]/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/io/mod.rs:1368:1
|
1368 | pub trait Write {
| --------------- doesn't satisfy `dyn std::io::Write: Sized`
|
= note: the following trait bounds were not satisfied:
`dyn std::io::Write: Sized`
I've been poking at the problem for the better part of a day. Either I'm missing something fundamental, or the special language treatment of Box is at play here (but I don't see how).