From f2082d2a04b3b5d72503ac89e2182a5833bb2a1e Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Sat, 19 Oct 2024 09:23:00 -0700 Subject: [PATCH] Clearer order of comparisons We look at seq.first/map.first only once. --- src/de.rs | 68 ++++++++++++++++++++++++------------------------------- 1 file changed, 30 insertions(+), 38 deletions(-) diff --git a/src/de.rs b/src/de.rs index a22fcf0de..5b64138d8 100644 --- a/src/de.rs +++ b/src/de.rs @@ -1936,24 +1936,20 @@ impl<'de, 'a, R: Read<'de> + 'a> de::SeqAccess<'de> for SeqAccess<'a, R> { } }; - match peek { - b']' => Ok(false), - b',' if !seq.first => { - 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)), - } - } - _ => { - if seq.first { - seq.first = false; - Ok(true) - } else { - Err(seq.de.peek_error(ErrorCode::ExpectedListCommaOrEnd)) - } + 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)) } } @@ -1991,29 +1987,25 @@ impl<'de, 'a, R: Read<'de> + 'a> de::MapAccess<'de> for MapAccess<'a, R> { } }; - match peek { - b'}' => Ok(false), - b',' if !map.first => { - 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)), - } + 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)) } - _ => { - if map.first { - map.first = false; - if peek == b'"' { - Ok(true) - } else { - Err(map.de.peek_error(ErrorCode::KeyMustBeAString)) - } - } else { - Err(map.de.peek_error(ErrorCode::ExpectedObjectCommaOrEnd)) - } + } 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)) } }