Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get rid of unwrap() for array parsing in generated code #781

Merged
merged 1 commit into from
Dec 29, 2022

Conversation

psychon
Copy link
Owner

@psychon psychon commented Dec 29, 2022

The generated code for parsing an array first got a slice of the expected length and then used the TryFrom trait to get an array. For example:

  let (data1, remaining) = crate::x11_utils::parse_u8_list(remaining, 24)?;
  let data1 = <[u8; 24]>::try_from(data1).unwrap();

This commit introduces a helper function that produces an array directly. The above becomes:

  let (data1, remaining) = crate::x11_utils::parse_u8_array::<24>(remaining)?;

The goal here is to remove some unwrap()s in the generated code, but this also helps shortening the code.

There are two new helper function: parse_u8_array parses an array while parse_u8_array_ref returns a reference to an array. Both cases are needed in the generated code.

Signed-off-by: Uli Schlachter psychon@znc.in

The generated code for parsing an array first got a slice of the
expected length and then used the TryFrom trait to get an array. For
example:

  let (data1, remaining) = crate::x11_utils::parse_u8_list(remaining, 24)?;
  let data1 = <[u8; 24]>::try_from(data1).unwrap();

This commit introduces a helper function that produces an array directly. The
above becomes:

  let (data1, remaining) = crate::x11_utils::parse_u8_array::<24>(remaining)?;

The goal here is to remove some unwrap()s in the generated code, but
this also helps shortening the code.

There are two new helper function: parse_u8_array parses an array while
parse_u8_array_ref returns a reference to an array. Both cases are
needed in the generated code.

Signed-off-by: Uli Schlachter <psychon@znc.in>
@codecov
Copy link

codecov bot commented Dec 29, 2022

Codecov Report

Base: 13.66% // Head: 13.67% // Increases project coverage by +0.00% 🎉

Coverage data is based on head (ea1b622) compared to base (a65dc71).
Patch coverage: 36.17% of modified lines in pull request are covered.

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #781   +/-   ##
=======================================
  Coverage   13.66%   13.67%           
=======================================
  Files         142      142           
  Lines      120042   120012   -30     
=======================================
+ Hits        16404    16407    +3     
+ Misses     103638   103605   -33     
Flag Coverage Δ
tests 13.67% <36.17%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
x11rb-protocol/src/protocol/glx.rs 0.00% <0.00%> (ø)
x11rb-protocol/src/protocol/xinput.rs 0.27% <0.00%> (+<0.01%) ⬆️
x11rb-protocol/src/protocol/xkb.rs 0.00% <0.00%> (ø)
x11rb-protocol/src/protocol/xv.rs 0.00% <0.00%> (ø)
x11rb-protocol/src/protocol/xvmc.rs 0.00% <0.00%> (ø)
x11rb-protocol/src/protocol/xproto.rs 5.13% <20.00%> (-0.01%) ⬇️
generator/src/generator/namespace/parse.rs 99.29% <100.00%> (-0.03%) ⬇️
x11rb-protocol/src/x11_utils.rs 82.40% <100.00%> (+0.69%) ⬆️
... and 1 more

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

@mergify mergify bot merged commit 11a9dca into master Dec 29, 2022
@mergify mergify bot deleted the array-unwrap branch December 29, 2022 14:14
@eduardosm
Copy link
Collaborator

For future reference, <T>::split_array_ref (currently unstable) would allow to implement parse_u8_array_ref without any unwrapping.

@psychon
Copy link
Owner Author

psychon commented Dec 29, 2022

Ah, nice. Thanks. I added that to #538.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants