Eliminate uses of expect in ssz_snappy.rs (#2105)

## Issue Addressed

None

## Proposed Changes

Eliminate three uses of `expect` in `ssz_snappy.rs`.

## Additional Info

None
This commit is contained in:
Samuel E. Moelius 2020-12-22 02:28:37 +00:00
parent 166f617b19
commit 3381266998

View File

@ -94,19 +94,20 @@ impl<TSpec: EthSpec> Decoder for SSZSnappyInboundCodec<TSpec> {
type Error = RPCError; type Error = RPCError;
fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> { fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> {
if self.len.is_none() { let length = if let Some(length) = self.len {
length
} else {
// Decode the length of the uncompressed bytes from an unsigned varint // Decode the length of the uncompressed bytes from an unsigned varint
// Note: length-prefix of > 10 bytes(uint64) would be a decoding error // Note: length-prefix of > 10 bytes(uint64) would be a decoding error
match self.inner.decode(src).map_err(RPCError::from)? { match self.inner.decode(src).map_err(RPCError::from)? {
Some(length) => { Some(length) => {
self.len = Some(length); self.len = Some(length);
length
} }
None => return Ok(None), // need more bytes to decode length None => return Ok(None), // need more bytes to decode length
} }
}; };
let length = self.len.expect("length should be Some");
// Should not attempt to decode rpc chunks with `length > max_packet_size` or not within bounds of // Should not attempt to decode rpc chunks with `length > max_packet_size` or not within bounds of
// packet size for ssz container corresponding to `self.protocol`. // packet size for ssz container corresponding to `self.protocol`.
let ssz_limits = self.protocol.rpc_request_limits(); let ssz_limits = self.protocol.rpc_request_limits();
@ -245,19 +246,20 @@ impl<TSpec: EthSpec> Decoder for SSZSnappyOutboundCodec<TSpec> {
type Error = RPCError; type Error = RPCError;
fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> { fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> {
if self.len.is_none() { let length = if let Some(length) = self.len {
length
} else {
// Decode the length of the uncompressed bytes from an unsigned varint // Decode the length of the uncompressed bytes from an unsigned varint
// Note: length-prefix of > 10 bytes(uint64) would be a decoding error // Note: length-prefix of > 10 bytes(uint64) would be a decoding error
match self.inner.decode(src).map_err(RPCError::from)? { match self.inner.decode(src).map_err(RPCError::from)? {
Some(length) => { Some(length) => {
self.len = Some(length as usize); self.len = Some(length as usize);
length
} }
None => return Ok(None), // need more bytes to decode length None => return Ok(None), // need more bytes to decode length
} }
}; };
let length = self.len.expect("length should be Some");
// Should not attempt to decode rpc chunks with `length > max_packet_size` or not within bounds of // Should not attempt to decode rpc chunks with `length > max_packet_size` or not within bounds of
// packet size for ssz container corresponding to `self.protocol`. // packet size for ssz container corresponding to `self.protocol`.
let ssz_limits = self.protocol.rpc_response_limits::<TSpec>(); let ssz_limits = self.protocol.rpc_response_limits::<TSpec>();
@ -323,18 +325,19 @@ impl<TSpec: EthSpec> OutboundCodec<RPCRequest<TSpec>> for SSZSnappyOutboundCodec
&mut self, &mut self,
src: &mut BytesMut, src: &mut BytesMut,
) -> Result<Option<Self::CodecErrorType>, RPCError> { ) -> Result<Option<Self::CodecErrorType>, RPCError> {
if self.len.is_none() { let length = if let Some(length) = self.len {
length
} else {
// Decode the length of the uncompressed bytes from an unsigned varint // Decode the length of the uncompressed bytes from an unsigned varint
match self.inner.decode(src).map_err(RPCError::from)? { match self.inner.decode(src).map_err(RPCError::from)? {
Some(length) => { Some(length) => {
self.len = Some(length as usize); self.len = Some(length as usize);
length
} }
None => return Ok(None), // need more bytes to decode length None => return Ok(None), // need more bytes to decode length
} }
}; };
let length = self.len.expect("length should be Some");
// Should not attempt to decode rpc chunks with `length > max_packet_size` or not within bounds of // Should not attempt to decode rpc chunks with `length > max_packet_size` or not within bounds of
// packet size for ssz container corresponding to `ErrorType`. // packet size for ssz container corresponding to `ErrorType`.
if length > self.max_packet_size || length > *ERROR_TYPE_MAX || length < *ERROR_TYPE_MIN { if length > self.max_packet_size || length > *ERROR_TYPE_MAX || length < *ERROR_TYPE_MIN {