Skip to content

Commit f2082d2

Browse files
committed
Clearer order of comparisons
We look at seq.first/map.first only once.
1 parent 0f54a1a commit f2082d2

File tree

1 file changed

+30
-38
lines changed

1 file changed

+30
-38
lines changed

src/de.rs

Lines changed: 30 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1936,24 +1936,20 @@ impl<'de, 'a, R: Read<'de> + 'a> de::SeqAccess<'de> for SeqAccess<'a, R> {
19361936
}
19371937
};
19381938

1939-
match peek {
1940-
b']' => Ok(false),
1941-
b',' if !seq.first => {
1942-
seq.de.eat_char();
1943-
match tri!(seq.de.parse_whitespace()) {
1944-
Some(b']') => Err(seq.de.peek_error(ErrorCode::TrailingComma)),
1945-
Some(_) => Ok(true),
1946-
None => Err(seq.de.peek_error(ErrorCode::EofWhileParsingValue)),
1947-
}
1948-
}
1949-
_ => {
1950-
if seq.first {
1951-
seq.first = false;
1952-
Ok(true)
1953-
} else {
1954-
Err(seq.de.peek_error(ErrorCode::ExpectedListCommaOrEnd))
1955-
}
1939+
if peek == b']' {
1940+
Ok(false)
1941+
} else if seq.first {
1942+
seq.first = false;
1943+
Ok(true)
1944+
} else if peek == b',' {
1945+
seq.de.eat_char();
1946+
match tri!(seq.de.parse_whitespace()) {
1947+
Some(b']') => Err(seq.de.peek_error(ErrorCode::TrailingComma)),
1948+
Some(_) => Ok(true),
1949+
None => Err(seq.de.peek_error(ErrorCode::EofWhileParsingValue)),
19561950
}
1951+
} else {
1952+
Err(seq.de.peek_error(ErrorCode::ExpectedListCommaOrEnd))
19571953
}
19581954
}
19591955

@@ -1991,29 +1987,25 @@ impl<'de, 'a, R: Read<'de> + 'a> de::MapAccess<'de> for MapAccess<'a, R> {
19911987
}
19921988
};
19931989

1994-
match peek {
1995-
b'}' => Ok(false),
1996-
b',' if !map.first => {
1997-
map.de.eat_char();
1998-
match tri!(map.de.parse_whitespace()) {
1999-
Some(b'"') => Ok(true),
2000-
Some(b'}') => Err(map.de.peek_error(ErrorCode::TrailingComma)),
2001-
Some(_) => Err(map.de.peek_error(ErrorCode::KeyMustBeAString)),
2002-
None => Err(map.de.peek_error(ErrorCode::EofWhileParsingValue)),
2003-
}
1990+
if peek == b'}' {
1991+
Ok(false)
1992+
} else if map.first {
1993+
map.first = false;
1994+
if peek == b'"' {
1995+
Ok(true)
1996+
} else {
1997+
Err(map.de.peek_error(ErrorCode::KeyMustBeAString))
20041998
}
2005-
_ => {
2006-
if map.first {
2007-
map.first = false;
2008-
if peek == b'"' {
2009-
Ok(true)
2010-
} else {
2011-
Err(map.de.peek_error(ErrorCode::KeyMustBeAString))
2012-
}
2013-
} else {
2014-
Err(map.de.peek_error(ErrorCode::ExpectedObjectCommaOrEnd))
2015-
}
1999+
} else if peek == b',' {
2000+
map.de.eat_char();
2001+
match tri!(map.de.parse_whitespace()) {
2002+
Some(b'"') => Ok(true),
2003+
Some(b'}') => Err(map.de.peek_error(ErrorCode::TrailingComma)),
2004+
Some(_) => Err(map.de.peek_error(ErrorCode::KeyMustBeAString)),
2005+
None => Err(map.de.peek_error(ErrorCode::EofWhileParsingValue)),
20162006
}
2007+
} else {
2008+
Err(map.de.peek_error(ErrorCode::ExpectedObjectCommaOrEnd))
20172009
}
20182010
}
20192011

0 commit comments

Comments
 (0)