From 1064e9245a584fd6ebd869c0b6aff02279b191cf Mon Sep 17 00:00:00 2001 From: Uli Schlachter Date: Thu, 17 Aug 2023 13:20:58 +0200 Subject: [PATCH 1/2] Use ? on Option where helpful Instead of using "match" and "return", we can just use "?" to simplify this code. Signed-off-by: Uli Schlachter --- x11rb/src/cookie.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/x11rb/src/cookie.rs b/x11rb/src/cookie.rs index 8131b3a8..29e8cf3e 100644 --- a/x11rb/src/cookie.rs +++ b/x11rb/src/cookie.rs @@ -341,10 +341,7 @@ macro_rules! multiple_reply_cookie { type Item = Result<$reply, ReplyError>; fn next(&mut self) -> Option { - let cookie = match self.0.take() { - None => return None, - Some(cookie) => cookie, - }; + let cookie = self.0.take()?; let reply = cookie .connection .wait_for_reply_or_error(cookie.sequence_number); From 6bc90f27fd04b7dbf4aed15c9926437cda9d2d2c Mon Sep 17 00:00:00 2001 From: Uli Schlachter Date: Thu, 17 Aug 2023 13:24:28 +0200 Subject: [PATCH 2/2] Avoid unwrap() in properties.rs This code has a constant with value 18. So far, this constant was kept as a u32. There is one place that needs this value as a usize. This conversion was done with try_into().unwrap(). This commit removes that unwrap() by changing the type of the constant to u16, which can be converted to usize via into(). Signed-off-by: Uli Schlachter --- x11rb/src/properties.rs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/x11rb/src/properties.rs b/x11rb/src/properties.rs index c1ccfa86..38e63f8f 100644 --- a/x11rb/src/properties.rs +++ b/x11rb/src/properties.rs @@ -166,7 +166,7 @@ property_cookie! { |reply| WmSizeHints::from_reply(&reply), } -const NUM_WM_SIZE_HINTS_ELEMENTS: u32 = 18; +const NUM_WM_SIZE_HINTS_ELEMENTS: u16 = 18; impl<'a, Conn> WmSizeHintsCookie<'a, Conn> where @@ -185,7 +185,7 @@ where property, AtomEnum::WM_SIZE_HINTS, 0, - NUM_WM_SIZE_HINTS_ELEMENTS, + NUM_WM_SIZE_HINTS_ELEMENTS.into(), )?)) } } @@ -332,7 +332,7 @@ impl WmSizeHints { property.into(), AtomEnum::WM_SIZE_HINTS, 32, - NUM_WM_SIZE_HINTS_ELEMENTS, + NUM_WM_SIZE_HINTS_ELEMENTS.into(), &data, ) } @@ -397,8 +397,7 @@ impl TryParse for WmSizeHints { impl Serialize for WmSizeHints { type Bytes = Vec; fn serialize(&self) -> Self::Bytes { - // 18*4 surely fits into an usize, so this unwrap() cannot trigger - let mut result = Vec::with_capacity((NUM_WM_SIZE_HINTS_ELEMENTS * 4).try_into().unwrap()); + let mut result = Vec::with_capacity((NUM_WM_SIZE_HINTS_ELEMENTS * 4).into()); self.serialize_into(&mut result); result }