Skip to content

Instantly share code, notes, and snippets.

@emk
Created October 2, 2014 16:42
Show Gist options
  • Save emk/a4de4cd324383b311ec0 to your computer and use it in GitHub Desktop.
Save emk/a4de4cd324383b311ec0 to your computer and use it in GitHub Desktop.
A tricky ownership issue with fill_buf
impl<'a,T: Buffer+'a> Buffer for ChunkBuffer<'a,T> {
fn fill_buf<'a>(&'a mut self) -> IoResult<&'a [u8]> {
if self.buffer.as_slice().contains_slice(self.boundary.as_slice()) {
// Exit 1: Valid data in our local buffer.
Ok(self.buffer.as_slice())
} else if self.buffer.len() > 0 {
// Exit 2: Add some more data to our local buffer so that it's
// valid (see invariants for top_up).
self.top_up()
} else {
{
// Exit 3: Exit on error.
let read = try!(self.input.fill_buf());
if read.contains_slice(self.boundary.as_slice()) {
// Exit 4: Valid input from self.input. Yay!
return Ok(read);
}
}
// Exit 4: Accumulate sufficient data in our local buffer (see
// invariants for top_up).
self.top_up()
}
}
/home/emk/w/src/rust-opus/src/buffer.rs:168:13: 168:17 error: cannot borrow `*self` as mutable more than once at a time
/home/emk/w/src/rust-opus/src/buffer.rs:168 self.top_up()
^~~~
/home/emk/w/src/rust-opus/src/buffer.rs:160:33: 160:43 note: previous borrow of `*self.input` occurs here; the mutable borrow prevents subsequent moves, borrows, or modification of `*self.input` until the borrow ends
/home/emk/w/src/rust-opus/src/buffer.rs:160 let read = try!(self.input.fill_buf());
^~~~~~~~~~
<std macros>:1:1: 3:2 note: in expansion of try!
/home/emk/w/src/rust-opus/src/buffer.rs:160:28: 160:56 note: expansion site
/home/emk/w/src/rust-opus/src/buffer.rs:170:6: 170:6 note: previous borrow ends here
/home/emk/w/src/rust-opus/src/buffer.rs:149 fn fill_buf<'a>(&'a mut self) -> IoResult<&'a [u8]> {
...
/home/emk/w/src/rust-opus/src/buffer.rs:170 }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment