Skip to content

Commit

Permalink
Generated code: Simpler conversion to usize
Browse files Browse the repository at this point in the history
This commit adds a crate-internal trait that allows to shorten
x.try_into().or(Err(ParseError::ConversionFailed)) to x.try_to_usize().
This also makes the code generator use this new trait/function.

Thanks to this change, many of the affected expressions now actually fit
into a single line on my screen!

Signed-off-by: Uli Schlachter <[email protected]>
  • Loading branch information
psychon committed Dec 31, 2020
1 parent 42aa0e3 commit 9efb03b
Show file tree
Hide file tree
Showing 33 changed files with 545 additions and 526 deletions.
6 changes: 3 additions & 3 deletions generator/src/generator/namespace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,7 @@ impl<'ns, 'c> NamespaceGenerator<'ns, 'c> {
outln!(out, "#[allow(unused_imports)]");
outln!(
out,
"use crate::x11_utils::{{Request, RequestHeader, Serialize, TryParse, TryParseFd}};"
"use crate::x11_utils::{{Request, RequestHeader, Serialize, TryParse, TryParseFd, TryIntoUSize}};"
);
outln!(
out,
Expand Down Expand Up @@ -4056,7 +4056,7 @@ impl<'ns, 'c> NamespaceGenerator<'ns, 'c> {
outln!(
out,
"let ({}, remaining) = crate::x11_utils::parse_u8_list({}, \
{}.try_into().or(Err(ParseError::ConversionFailed))?)?;",
{}.try_to_usize()?)?;",
rust_field_name,
from,
self.expr_to_str(
Expand Down Expand Up @@ -4098,7 +4098,7 @@ impl<'ns, 'c> NamespaceGenerator<'ns, 'c> {
outln!(
out,
"let ({}, remaining) = crate::x11_utils::parse_list::<{}>(remaining, \
{}.try_into().or(Err(ParseError::ConversionFailed))?)?;",
{}.try_to_usize()?)?;",
rust_field_name,
rust_element_type,
self.expr_to_str(
Expand Down
2 changes: 1 addition & 1 deletion src/protocol/bigreq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use std::io::IoSlice;
#[allow(unused_imports)]
use crate::utils::{RawFdContainer, pretty_print_bitmask, pretty_print_enum};
#[allow(unused_imports)]
use crate::x11_utils::{Request, RequestHeader, Serialize, TryParse, TryParseFd};
use crate::x11_utils::{Request, RequestHeader, Serialize, TryParse, TryParseFd, TryIntoUSize};
use crate::connection::{BufWithFds, PiecewiseBuf, RequestConnection};
#[allow(unused_imports)]
use crate::cookie::{Cookie, CookieWithFds, VoidCookie};
Expand Down
2 changes: 1 addition & 1 deletion src/protocol/composite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use std::io::IoSlice;
#[allow(unused_imports)]
use crate::utils::{RawFdContainer, pretty_print_bitmask, pretty_print_enum};
#[allow(unused_imports)]
use crate::x11_utils::{Request, RequestHeader, Serialize, TryParse, TryParseFd};
use crate::x11_utils::{Request, RequestHeader, Serialize, TryParse, TryParseFd, TryIntoUSize};
use crate::connection::{BufWithFds, PiecewiseBuf, RequestConnection};
#[allow(unused_imports)]
use crate::cookie::{Cookie, CookieWithFds, VoidCookie};
Expand Down
2 changes: 1 addition & 1 deletion src/protocol/damage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use std::io::IoSlice;
#[allow(unused_imports)]
use crate::utils::{RawFdContainer, pretty_print_bitmask, pretty_print_enum};
#[allow(unused_imports)]
use crate::x11_utils::{Request, RequestHeader, Serialize, TryParse, TryParseFd};
use crate::x11_utils::{Request, RequestHeader, Serialize, TryParse, TryParseFd, TryIntoUSize};
use crate::connection::{BufWithFds, PiecewiseBuf, RequestConnection};
#[allow(unused_imports)]
use crate::cookie::{Cookie, CookieWithFds, VoidCookie};
Expand Down
2 changes: 1 addition & 1 deletion src/protocol/dpms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use std::io::IoSlice;
#[allow(unused_imports)]
use crate::utils::{RawFdContainer, pretty_print_bitmask, pretty_print_enum};
#[allow(unused_imports)]
use crate::x11_utils::{Request, RequestHeader, Serialize, TryParse, TryParseFd};
use crate::x11_utils::{Request, RequestHeader, Serialize, TryParse, TryParseFd, TryIntoUSize};
use crate::connection::{BufWithFds, PiecewiseBuf, RequestConnection};
#[allow(unused_imports)]
use crate::cookie::{Cookie, CookieWithFds, VoidCookie};
Expand Down
12 changes: 6 additions & 6 deletions src/protocol/dri2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use std::io::IoSlice;
#[allow(unused_imports)]
use crate::utils::{RawFdContainer, pretty_print_bitmask, pretty_print_enum};
#[allow(unused_imports)]
use crate::x11_utils::{Request, RequestHeader, Serialize, TryParse, TryParseFd};
use crate::x11_utils::{Request, RequestHeader, Serialize, TryParse, TryParseFd, TryIntoUSize};
use crate::connection::{BufWithFds, PiecewiseBuf, RequestConnection};
#[allow(unused_imports)]
use crate::cookie::{Cookie, CookieWithFds, VoidCookie};
Expand Down Expand Up @@ -509,11 +509,11 @@ impl TryParse for ConnectReply {
let (driver_name_length, remaining) = u32::try_parse(remaining)?;
let (device_name_length, remaining) = u32::try_parse(remaining)?;
let remaining = remaining.get(16..).ok_or(ParseError::InsufficientData)?;
let (driver_name, remaining) = crate::x11_utils::parse_u8_list(remaining, driver_name_length.try_into().or(Err(ParseError::ConversionFailed))?)?;
let (driver_name, remaining) = crate::x11_utils::parse_u8_list(remaining, driver_name_length.try_to_usize()?)?;
let driver_name = driver_name.to_vec();
let (alignment_pad, remaining) = crate::x11_utils::parse_u8_list(remaining, (driver_name_length.checked_add(3u32).ok_or(ParseError::InvalidExpression)? & (!3u32)).checked_sub(driver_name_length).ok_or(ParseError::InvalidExpression)?.try_into().or(Err(ParseError::ConversionFailed))?)?;
let (alignment_pad, remaining) = crate::x11_utils::parse_u8_list(remaining, (driver_name_length.checked_add(3u32).ok_or(ParseError::InvalidExpression)? & (!3u32)).checked_sub(driver_name_length).ok_or(ParseError::InvalidExpression)?.try_to_usize()?)?;
let alignment_pad = alignment_pad.to_vec();
let (device_name, remaining) = crate::x11_utils::parse_u8_list(remaining, device_name_length.try_into().or(Err(ParseError::ConversionFailed))?)?;
let (device_name, remaining) = crate::x11_utils::parse_u8_list(remaining, device_name_length.try_to_usize()?)?;
let device_name = device_name.to_vec();
if response_type != 1 {
return Err(ParseError::InvalidValue);
Expand Down Expand Up @@ -911,7 +911,7 @@ impl TryParse for GetBuffersReply {
let (height, remaining) = u32::try_parse(remaining)?;
let (count, remaining) = u32::try_parse(remaining)?;
let remaining = remaining.get(12..).ok_or(ParseError::InsufficientData)?;
let (buffers, remaining) = crate::x11_utils::parse_list::<DRI2Buffer>(remaining, count.try_into().or(Err(ParseError::ConversionFailed))?)?;
let (buffers, remaining) = crate::x11_utils::parse_list::<DRI2Buffer>(remaining, count.try_to_usize()?)?;
if response_type != 1 {
return Err(ParseError::InvalidValue);
}
Expand Down Expand Up @@ -1181,7 +1181,7 @@ impl TryParse for GetBuffersWithFormatReply {
let (height, remaining) = u32::try_parse(remaining)?;
let (count, remaining) = u32::try_parse(remaining)?;
let remaining = remaining.get(12..).ok_or(ParseError::InsufficientData)?;
let (buffers, remaining) = crate::x11_utils::parse_list::<DRI2Buffer>(remaining, count.try_into().or(Err(ParseError::ConversionFailed))?)?;
let (buffers, remaining) = crate::x11_utils::parse_list::<DRI2Buffer>(remaining, count.try_to_usize()?)?;
if response_type != 1 {
return Err(ParseError::InvalidValue);
}
Expand Down
10 changes: 5 additions & 5 deletions src/protocol/dri3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use std::io::IoSlice;
#[allow(unused_imports)]
use crate::utils::{RawFdContainer, pretty_print_bitmask, pretty_print_enum};
#[allow(unused_imports)]
use crate::x11_utils::{Request, RequestHeader, Serialize, TryParse, TryParseFd};
use crate::x11_utils::{Request, RequestHeader, Serialize, TryParse, TryParseFd, TryIntoUSize};
use crate::connection::{BufWithFds, PiecewiseBuf, RequestConnection};
#[allow(unused_imports)]
use crate::cookie::{Cookie, CookieWithFds, VoidCookie};
Expand Down Expand Up @@ -783,8 +783,8 @@ impl TryParse for GetSupportedModifiersReply {
let (num_window_modifiers, remaining) = u32::try_parse(remaining)?;
let (num_screen_modifiers, remaining) = u32::try_parse(remaining)?;
let remaining = remaining.get(16..).ok_or(ParseError::InsufficientData)?;
let (window_modifiers, remaining) = crate::x11_utils::parse_list::<u64>(remaining, num_window_modifiers.try_into().or(Err(ParseError::ConversionFailed))?)?;
let (screen_modifiers, remaining) = crate::x11_utils::parse_list::<u64>(remaining, num_screen_modifiers.try_into().or(Err(ParseError::ConversionFailed))?)?;
let (window_modifiers, remaining) = crate::x11_utils::parse_list::<u64>(remaining, num_window_modifiers.try_to_usize()?)?;
let (screen_modifiers, remaining) = crate::x11_utils::parse_list::<u64>(remaining, num_screen_modifiers.try_to_usize()?)?;
if response_type != 1 {
return Err(ParseError::InvalidValue);
}
Expand Down Expand Up @@ -1125,8 +1125,8 @@ impl TryParseFd for BuffersFromPixmapReply {
let (depth, remaining) = u8::try_parse(remaining)?;
let (bpp, remaining) = u8::try_parse(remaining)?;
let remaining = remaining.get(6..).ok_or(ParseError::InsufficientData)?;
let (strides, remaining) = crate::x11_utils::parse_list::<u32>(remaining, nfd.try_into().or(Err(ParseError::ConversionFailed))?)?;
let (offsets, remaining) = crate::x11_utils::parse_list::<u32>(remaining, nfd.try_into().or(Err(ParseError::ConversionFailed))?)?;
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))?;
if fds.len() < fds_len { return Err(ParseError::MissingFileDescriptors) }
let mut buffers = fds.split_off(fds_len);
Expand Down
2 changes: 1 addition & 1 deletion src/protocol/ge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use std::io::IoSlice;
#[allow(unused_imports)]
use crate::utils::{RawFdContainer, pretty_print_bitmask, pretty_print_enum};
#[allow(unused_imports)]
use crate::x11_utils::{Request, RequestHeader, Serialize, TryParse, TryParseFd};
use crate::x11_utils::{Request, RequestHeader, Serialize, TryParse, TryParseFd, TryIntoUSize};
use crate::connection::{BufWithFds, PiecewiseBuf, RequestConnection};
#[allow(unused_imports)]
use crate::cookie::{Cookie, CookieWithFds, VoidCookie};
Expand Down
Loading

0 comments on commit 9efb03b

Please sign in to comment.