I'm getting an error telling me to remove the ?Sized bound, but I'd really like to keep it. I want the generic function to accept either a concrete sized type (and make trait objects where needed), or take something that is already a dyn reference.
fn test() {
allow_unsized(&mut vec![0u8] as &mut dyn Write);
allow_unsized(&mut vec![0u8] as &mut Vec<u8>);
}
A &mut MaybeUnsized consists of a length and a pointer, and can't be coerced to a &mut dyn Write consisting of a vtable and a pointer, as the length would be discarded (and sizes stored in vtables are static).
Your work-around works because you're now doing unsizing coercion on a &mut &mut Write + ?Sized and not a &mut Write + ?Sized (and Write is implemented for &mut Write + ?Sized). I.e. you got rid of the deref. The mut w is required to take the outer &mut reference to w.