Skip to content

Commit

Permalink
Use try_to_usize() in some more cases
Browse files Browse the repository at this point in the history
This commit deals with two / four special cases that were not dealt with
in the previous commit yet.

Signed-off-by: Uli Schlachter <[email protected]>
  • Loading branch information
psychon committed Dec 31, 2020
1 parent 9efb03b commit df85d2d
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 6 deletions.
4 changes: 2 additions & 2 deletions generator/src/generator/namespace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4132,7 +4132,7 @@ impl<'ns, 'c> NamespaceGenerator<'ns, 'c> {
outln!(
out,
"let list_length = \
usize::try_from({}).or(Err(ParseError::ConversionFailed))?;",
{}.try_to_usize()?;",
self.expr_to_str(
length_expr,
to_rust_variable_name,
Expand Down Expand Up @@ -4198,7 +4198,7 @@ impl<'ns, 'c> NamespaceGenerator<'ns, 'c> {

outln!(
out,
"let fds_len = usize::try_from({}).or(Err(ParseError::ConversionFailed))?;",
"let fds_len = {}.try_to_usize()?;",
self.expr_to_str(
&fd_list_field.length_expr,
to_rust_variable_name,
Expand Down
4 changes: 2 additions & 2 deletions src/protocol/dri3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -980,7 +980,7 @@ impl PixmapFromBuffersRequest {
let (bpp, remaining) = u8::try_parse(remaining)?;
let remaining = remaining.get(2..).ok_or(ParseError::InsufficientData)?;
let (modifier, remaining) = u64::try_parse(remaining)?;
let fds_len = usize::try_from(num_buffers).or(Err(ParseError::ConversionFailed))?;
let fds_len = num_buffers.try_to_usize()?;
if fds.len() < fds_len { return Err(ParseError::MissingFileDescriptors) }
let mut buffers = fds.split_off(fds_len);
std::mem::swap(fds, &mut buffers);
Expand Down Expand Up @@ -1127,7 +1127,7 @@ impl TryParseFd for BuffersFromPixmapReply {
let remaining = remaining.get(6..).ok_or(ParseError::InsufficientData)?;
let (strides, remaining) = crate::x11_utils::parse_list::<u32>(remaining, nfd.try_to_usize()?)?;
let (offsets, remaining) = crate::x11_utils::parse_list::<u32>(remaining, nfd.try_to_usize()?)?;
let fds_len = usize::try_from(nfd).or(Err(ParseError::ConversionFailed))?;
let fds_len = nfd.try_to_usize()?;
if fds.len() < fds_len { return Err(ParseError::MissingFileDescriptors) }
let mut buffers = fds.split_off(fds_len);
std::mem::swap(fds, &mut buffers);
Expand Down
2 changes: 1 addition & 1 deletion src/protocol/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1498,7 +1498,7 @@ impl TryParse for QueryPictFormatsReply {
let (formats, remaining) = crate::x11_utils::parse_list::<Pictforminfo>(remaining, num_formats.try_to_usize()?)?;
let (screens, remaining) = crate::x11_utils::parse_list::<Pictscreen>(remaining, num_screens.try_to_usize()?)?;
let mut remaining = remaining;
let list_length = usize::try_from(num_subpixel).or(Err(ParseError::ConversionFailed))?;
let list_length = num_subpixel.try_to_usize()?;
let mut subpixels = Vec::with_capacity(list_length);
for _ in 0..list_length {
let (v, new_remaining) = u32::try_parse(remaining)?;
Expand Down
2 changes: 1 addition & 1 deletion src/protocol/xinput.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2014,7 +2014,7 @@ impl TryParse for GetDeviceMotionEventsReply {
let (device_mode, remaining) = u8::try_parse(remaining)?;
let remaining = remaining.get(18..).ok_or(ParseError::InsufficientData)?;
let mut remaining = remaining;
let list_length = usize::try_from(num_events).or(Err(ParseError::ConversionFailed))?;
let list_length = num_events.try_to_usize()?;
let mut events = Vec::with_capacity(list_length);
for _ in 0..list_length {
let (v, new_remaining) = DeviceTimeCoord::try_parse(remaining, num_axes)?;
Expand Down

0 comments on commit df85d2d

Please sign in to comment.