Skip to content

Commit

Permalink
Auto merge of #18 - nikomatsakis:rfc1214-fixes, r=SimonSapin
Browse files Browse the repository at this point in the history
Update tendril to avoid double borrow.

Update tendril to avoid double borrow. This double borrow is detected by the latest Nightly builds, or at least will be once rust-lang/rust#27641 makes it into such a build.

<!-- Reviewable:start -->
[<img src="https://reviewable.io/review_button.png" height=40 alt="Review on Reviewable"/>](https://reviewable.io/reviews/servo/tendril/18)
<!-- Reviewable:end -->
  • Loading branch information
bors-servo committed Aug 15, 2015
2 parents 0de69e9 + 8fc15b5 commit a3a2afb
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ pub unsafe trait SubsetOf<Super>: Format

/// Indicates a format which corresponds to a Rust slice type,
/// representing exactly the same invariants.
pub unsafe trait SliceFormat: Format {
pub unsafe trait SliceFormat: Format + Sized {
type Slice: ?Sized + Slice<Format = Self>;
}

Expand Down
36 changes: 28 additions & 8 deletions src/tendril.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1178,15 +1178,35 @@ impl<F, A> Tendril<F, A>
#[inline]
pub fn pop_front_char<'a>(&'a mut self) -> Option<char> {
unsafe {
let mut it = F::char_indices(self.as_byte_slice());
it.next().map(|(_, c)| {
if let Some((n, _)) = it.next() {
self.unsafe_pop_front(n as u32);
} else {
self.clear();
let next_char; // first char in iterator
let mut skip = 0; // number of bytes to skip, or 0 to clear

{ // <--+
// | Creating an iterator borrows self, so introduce a
// +- scope to contain the borrow (that way we can mutate
// self below, after this scope exits).

let mut iter = F::char_indices(self.as_byte_slice());
match iter.next() {
Some((_, c)) => {
next_char = Some(c);
if let Some((n, _)) = iter.next() {
skip = n as u32;
}
}
None => {
next_char = None;
}
}
c
})
}

if skip != 0 {
self.unsafe_pop_front(skip);
} else {
self.clear();
}

next_char
}
}

Expand Down

0 comments on commit a3a2afb

Please sign in to comment.