From add7e4cca54ecc7ae0c70e3fece7099e6a3523de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Garillot?= <4142+huitseeker@users.noreply.github.com> Date: Sat, 29 Jan 2022 16:00:48 -0500 Subject: [PATCH] Clean up a few Option/Result patterns that fit an std combinator (cherry picked from commit f731019c8341105117016af9d4a5134f8146053e) --- quinn-proto/src/connection/assembler.rs | 5 +---- quinn-proto/src/connection/streams/mod.rs | 27 +++++++++++++---------- quinn/examples/server.rs | 2 +- 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/quinn-proto/src/connection/assembler.rs b/quinn-proto/src/connection/assembler.rs index 7cfd8757f..f0044970e 100644 --- a/quinn-proto/src/connection/assembler.rs +++ b/quinn-proto/src/connection/assembler.rs @@ -51,10 +51,7 @@ impl Assembler { /// Get the the next chunk pub(crate) fn read(&mut self, max_length: usize, ordered: bool) -> Option { loop { - let mut chunk = match self.data.peek_mut() { - Some(chunk) => chunk, - None => return None, - }; + let mut chunk = self.data.peek_mut()?; if ordered { if chunk.offset > self.bytes_read { diff --git a/quinn-proto/src/connection/streams/mod.rs b/quinn-proto/src/connection/streams/mod.rs index 2f6cc5264..41de6565d 100644 --- a/quinn-proto/src/connection/streams/mod.rs +++ b/quinn-proto/src/connection/streams/mod.rs @@ -254,10 +254,11 @@ impl<'a> SendStream<'a> { /// # Panics /// - when applied to a receive stream pub fn reset(&mut self, error_code: VarInt) -> Result<(), UnknownStream> { - let stream = match self.state.send.get_mut(&self.id) { - Some(ss) => ss, - None => return Err(UnknownStream { _private: () }), - }; + let stream = self + .state + .send + .get_mut(&self.id) + .ok_or(UnknownStream { _private: () })?; if matches!(stream.state, SendState::ResetSent) { // Redundant reset call @@ -280,10 +281,11 @@ impl<'a> SendStream<'a> { /// # Panics /// - when applied to a receive stream pub fn set_priority(&mut self, priority: i32) -> Result<(), UnknownStream> { - let stream = match self.state.send.get_mut(&self.id) { - Some(ss) => ss, - None => return Err(UnknownStream { _private: () }), - }; + let stream = self + .state + .send + .get_mut(&self.id) + .ok_or(UnknownStream { _private: () })?; stream.priority = priority; Ok(()) @@ -294,10 +296,11 @@ impl<'a> SendStream<'a> { /// # Panics /// - when applied to a receive stream pub fn priority(&self) -> Result { - let stream = match self.state.send.get(&self.id) { - Some(ss) => ss, - None => return Err(UnknownStream { _private: () }), - }; + let stream = self + .state + .send + .get(&self.id) + .ok_or(UnknownStream { _private: () })?; Ok(stream.priority) } diff --git a/quinn/examples/server.rs b/quinn/examples/server.rs index 49ed00368..4b9f89329 100644 --- a/quinn/examples/server.rs +++ b/quinn/examples/server.rs @@ -243,7 +243,7 @@ fn process_get(root: &Path, x: &[u8]) -> Result> { bail!("missing \\r\\n"); } let x = &x[4..x.len() - 2]; - let end = x.iter().position(|&c| c == b' ').unwrap_or_else(|| x.len()); + let end = x.iter().position(|&c| c == b' ').unwrap_or(x.len()); let path = str::from_utf8(&x[..end]).context("path is malformed UTF-8")?; let path = Path::new(&path); let mut real_path = PathBuf::from(root);