1
0
mirror of https://github.com/google/comprehensive-rust.git synced 2024-12-16 06:39:46 +02:00

Remove unnecessary syntax in protobuf example (#1592)

`Ok(x?)` has the same outcome as `x` and save an unpacking/repacking
cycle.

`x as usize` has no effect when `x` already has type `usize`.

In `x < 4usize` the `usize` is unnecessary when `x` already has type
`usize`.
This commit is contained in:
Martin Huschenbett 2023-12-14 16:02:40 +01:00 committed by GitHub
parent c7a86ca584
commit 302a03bbe3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -81,7 +81,7 @@ impl<'a> FieldValue<'a> {
let FieldValue::Len(data) = self else { let FieldValue::Len(data) = self else {
return Err(Error::UnexpectedWireType); return Err(Error::UnexpectedWireType);
}; };
Ok(std::str::from_utf8(data).map_err(|_| Error::InvalidString)?) std::str::from_utf8(data).map_err(|_| Error::InvalidString)
} }
fn as_bytes(&self) -> Result<&'a [u8], Error> { fn as_bytes(&self) -> Result<&'a [u8], Error> {
@ -142,14 +142,14 @@ fn parse_field(data: &[u8]) -> Result<(Field, &[u8]), Error> {
WireType::Len => { WireType::Len => {
let (len, remainder) = parse_varint(remainder)?; let (len, remainder) = parse_varint(remainder)?;
let len: usize = len.try_into()?; let len: usize = len.try_into()?;
if remainder.len() < len as usize { if remainder.len() < len {
return Err(Error::UnexpectedEOF); return Err(Error::UnexpectedEOF);
} }
let (value, remainder) = remainder.split_at(len); let (value, remainder) = remainder.split_at(len);
(FieldValue::Len(value), remainder) (FieldValue::Len(value), remainder)
} }
WireType::I32 => { WireType::I32 => {
if remainder.len() < 4usize { if remainder.len() < 4 {
return Err(Error::UnexpectedEOF); return Err(Error::UnexpectedEOF);
} }
let (value, remainder) = remainder.split_at(4); let (value, remainder) = remainder.split_at(4);