I can't figure out a way to not require type annotations in the Box
case, but using a couple of levels of indirection via Borrow
seem to work for storing it at least (Rust Playground)
use std::io;
use std::borrow::Borrow;
use std::marker::PhantomData;
struct Foo<T: io::Read + ?Sized = io::Read, U: Borrow<T> = Box<io::Read>> {
foo: U,
marker: PhantomData<T>,
}
impl<T: io::Read + ?Sized, U: Borrow<T>> Foo<T, U> {
fn new(foo: U) -> Foo<T, U> {
Foo {
foo: foo,
marker: PhantomData,
}
}
}
fn main() {
let a = io::Cursor::new(Vec::new());
let b = Foo::new(a.clone());
let c = Foo::<io::Read, Box<io::Read>>::new(Box::new(a) as Box<io::Read>);
}