Skip to content

Commit

Permalink
Merge pull request #1206 from dtolnay/hasnext
Browse files Browse the repository at this point in the history
Improve performance of seq/map peeks
  • Loading branch information
dtolnay authored Oct 19, 2024
2 parents 4cb90ce + f2082d2 commit f45b422
Showing 1 changed file with 35 additions and 39 deletions.
74 changes: 35 additions & 39 deletions src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1930,30 +1930,26 @@ impl<'de, 'a, R: Read<'de> + 'a> de::SeqAccess<'de> for SeqAccess<'a, R> {
seq: &mut SeqAccess<'a, R>,
) -> Result<bool> {
let peek = match tri!(seq.de.parse_whitespace()) {
Some(b']') => {
return Ok(false);
}
Some(b',') if !seq.first => {
seq.de.eat_char();
tri!(seq.de.parse_whitespace())
}
Some(b) => {
if seq.first {
seq.first = false;
Some(b)
} else {
return Err(seq.de.peek_error(ErrorCode::ExpectedListCommaOrEnd));
}
}
Some(b) => b,
None => {
return Err(seq.de.peek_error(ErrorCode::EofWhileParsingList));
}
};

match peek {
Some(b']') => Err(seq.de.peek_error(ErrorCode::TrailingComma)),
Some(_) => Ok(true),
None => Err(seq.de.peek_error(ErrorCode::EofWhileParsingValue)),
if peek == b']' {
Ok(false)
} else if seq.first {
seq.first = false;
Ok(true)
} else if peek == b',' {
seq.de.eat_char();
match tri!(seq.de.parse_whitespace()) {
Some(b']') => Err(seq.de.peek_error(ErrorCode::TrailingComma)),
Some(_) => Ok(true),
None => Err(seq.de.peek_error(ErrorCode::EofWhileParsingValue)),
}
} else {
Err(seq.de.peek_error(ErrorCode::ExpectedListCommaOrEnd))
}
}

Expand Down Expand Up @@ -1985,31 +1981,31 @@ impl<'de, 'a, R: Read<'de> + 'a> de::MapAccess<'de> for MapAccess<'a, R> {
{
fn has_next_key<'de, 'a, R: Read<'de> + 'a>(map: &mut MapAccess<'a, R>) -> Result<bool> {
let peek = match tri!(map.de.parse_whitespace()) {
Some(b'}') => {
return Ok(false);
}
Some(b',') if !map.first => {
map.de.eat_char();
tri!(map.de.parse_whitespace())
}
Some(b) => {
if map.first {
map.first = false;
Some(b)
} else {
return Err(map.de.peek_error(ErrorCode::ExpectedObjectCommaOrEnd));
}
}
Some(b) => b,
None => {
return Err(map.de.peek_error(ErrorCode::EofWhileParsingObject));
}
};

match peek {
Some(b'"') => Ok(true),
Some(b'}') => Err(map.de.peek_error(ErrorCode::TrailingComma)),
Some(_) => Err(map.de.peek_error(ErrorCode::KeyMustBeAString)),
None => Err(map.de.peek_error(ErrorCode::EofWhileParsingValue)),
if peek == b'}' {
Ok(false)
} else if map.first {
map.first = false;
if peek == b'"' {
Ok(true)
} else {
Err(map.de.peek_error(ErrorCode::KeyMustBeAString))
}
} else if peek == b',' {
map.de.eat_char();
match tri!(map.de.parse_whitespace()) {
Some(b'"') => Ok(true),
Some(b'}') => Err(map.de.peek_error(ErrorCode::TrailingComma)),
Some(_) => Err(map.de.peek_error(ErrorCode::KeyMustBeAString)),
None => Err(map.de.peek_error(ErrorCode::EofWhileParsingValue)),
}
} else {
Err(map.de.peek_error(ErrorCode::ExpectedObjectCommaOrEnd))
}
}

Expand Down

0 comments on commit f45b422

Please sign in to comment.