Skip to content

Commit

Permalink
Avoid unwrap() in properties.rs
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
psychon committed Aug 17, 2023
1 parent 1064e92 commit 6bc90f2
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions x11rb/src/properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -185,7 +185,7 @@ where
property,
AtomEnum::WM_SIZE_HINTS,
0,
NUM_WM_SIZE_HINTS_ELEMENTS,
NUM_WM_SIZE_HINTS_ELEMENTS.into(),
)?))
}
}
Expand Down Expand Up @@ -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,
)
}
Expand Down Expand Up @@ -397,8 +397,7 @@ impl TryParse for WmSizeHints {
impl Serialize for WmSizeHints {
type Bytes = Vec<u8>;
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
}
Expand Down

0 comments on commit 6bc90f2

Please sign in to comment.