Description
I figured that with the advent of traits working and &mut io::Writer
, we would want to write lots of code that only takes &mut io::Writer
to pass streams around.
It turns out though that if you squirrel these away in structs, they can't ever really get moved out. For example, this code fails to compile
use std::rt::io;
struct HttpResponse<'self> {
out: &'self mut io::Writer,
}
fn write_http_header(_: &mut io::Writer) {}
impl<'self> HttpResponse<'self> {
fn respond(&mut self) {
write_http_header(self.out);
}
}
fn main() {}
foo.rs:11:26: 11:34 error: cannot move out of dereference of & pointer
foo.rs:11 write_http_header(self.out);
^~~~~~~~
error: aborting due to previous error
The specific example I hit was when I was ripping out @::std::io::Writer
from std::repr
. At some point the visitor decides to give its buffer to a child visitor for the small duration of a call, and there's also a point where it shoves passes the buffer directly to an object's write_repr
method.
The errors are completely legitimate (given what I believe are today's region rules for &mut
). That being said however, it seems like a fairly common use-case. Tweaking the rules to realize that this is safe could be quite difficult, but perhaps it's possible?
If this doesn't turn out to be a common case, then there's really no need to deal with it. For now I've resorted to using transmute_copy
in a few places in #8414
cc @brson for io::Writer feedback
cc @nikomatsakis for if rules would be impossible to tweak