Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update tendril to avoid double borrow. #18

Merged
merged 1 commit into from
Aug 15, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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