From d944c657a208490af7fd5c0e793d8e4d46566dcc Mon Sep 17 00:00:00 2001 From: Andrew Fitzgerald Date: Wed, 1 Feb 2023 09:17:12 -0800 Subject: [PATCH] Use RangeBounds for is_within_range (#29763) --- bench-tps/src/cli.rs | 2 +- clap-utils/src/input_validators.rs | 10 ++++------ clap-v3-utils/src/input_validators.rs | 10 ++++------ cli/src/stake.rs | 2 +- cli/src/vote.rs | 2 +- validator/src/cli.rs | 4 ++-- 6 files changed, 13 insertions(+), 17 deletions(-) diff --git a/bench-tps/src/cli.rs b/bench-tps/src/cli.rs index 789f0a2d19..2195f697b6 100644 --- a/bench-tps/src/cli.rs +++ b/bench-tps/src/cli.rs @@ -352,7 +352,7 @@ pub fn build_args<'a>(version: &'_ str) -> App<'a, '_> { Arg::with_name("num_conflict_groups") .long("num-conflict-groups") .takes_value(true) - .validator(|arg| is_within_range(arg, 1, usize::MAX - 1)) + .validator(|arg| is_within_range(arg, 1..)) .help("The number of unique destination accounts per transactions 'chunk'. Lower values will result in more transaction conflicts.") ) } diff --git a/clap-utils/src/input_validators.rs b/clap-utils/src/input_validators.rs index c600d50176..2cb965903f 100644 --- a/clap-utils/src/input_validators.rs +++ b/clap-utils/src/input_validators.rs @@ -7,7 +7,7 @@ use { pubkey::{Pubkey, MAX_SEED_LEN}, signature::{read_keypair_file, Signature}, }, - std::{fmt::Display, str::FromStr}, + std::{fmt::Display, ops::RangeBounds, str::FromStr}, }; fn is_parsable_generic(string: T) -> Result<(), String> @@ -35,18 +35,16 @@ where // Return an error if string cannot be parsed as numeric type T, and value not within specified // range -pub fn is_within_range(string: String, range_min: T, range_max: T) -> Result<(), String> +pub fn is_within_range(string: String, range: R) -> Result<(), String> where T: FromStr + Copy + std::fmt::Debug + PartialOrd + std::ops::Add + From, T::Err: Display, + R: RangeBounds + std::fmt::Debug, { match string.parse::() { Ok(input) => { - let range = range_min..range_max + 1.into(); if !range.contains(&input) { - Err(format!( - "input '{input:?}' out of range ({range_min:?}..{range_max:?}]" - )) + Err(format!("input '{input:?}' out of range {range:?}")) } else { Ok(()) } diff --git a/clap-v3-utils/src/input_validators.rs b/clap-v3-utils/src/input_validators.rs index 78eef2b54c..cd016a209f 100644 --- a/clap-v3-utils/src/input_validators.rs +++ b/clap-v3-utils/src/input_validators.rs @@ -7,7 +7,7 @@ use { pubkey::{Pubkey, MAX_SEED_LEN}, signature::{read_keypair_file, Signature}, }, - std::{fmt::Display, str::FromStr}, + std::{fmt::Display, ops::RangeBounds, str::FromStr}, }; fn is_parsable_generic(string: T) -> Result<(), String> @@ -35,18 +35,16 @@ where // Return an error if string cannot be parsed as numeric type T, and value not within specified // range -pub fn is_within_range(string: String, range_min: T, range_max: T) -> Result<(), String> +pub fn is_within_range(string: String, range: R) -> Result<(), String> where T: FromStr + Copy + std::fmt::Debug + PartialOrd + std::ops::Add + From, T::Err: Display, + R: RangeBounds + std::fmt::Debug, { match string.parse::() { Ok(input) => { - let range = range_min..range_max + 1.into(); if !range.contains(&input) { - Err(format!( - "input '{input:?}' out of range ({range_min:?}..{range_max:?}]" - )) + Err(format!("input '{input:?}' out of range {range:?}")) } else { Ok(()) } diff --git a/cli/src/stake.rs b/cli/src/stake.rs index deddf382c4..05192544cf 100644 --- a/cli/src/stake.rs +++ b/cli/src/stake.rs @@ -686,7 +686,7 @@ impl StakeSubCommands for App<'_, '_> { .long("num-rewards-epochs") .takes_value(true) .value_name("NUM") - .validator(|s| is_within_range(s, 1, 10)) + .validator(|s| is_within_range(s, 1..=10)) .default_value_if("with_rewards", None, "1") .requires("with_rewards") .help("Display rewards for NUM recent epochs, max 10 [default: latest epoch only]"), diff --git a/cli/src/vote.rs b/cli/src/vote.rs index 0104e67649..afa344ec25 100644 --- a/cli/src/vote.rs +++ b/cli/src/vote.rs @@ -338,7 +338,7 @@ impl VoteSubCommands for App<'_, '_> { .long("num-rewards-epochs") .takes_value(true) .value_name("NUM") - .validator(|s| is_within_range(s, 1, 10)) + .validator(|s| is_within_range(s, 1..=10)) .default_value_if("with_rewards", None, "1") .requires("with_rewards") .help("Display rewards for NUM recent epochs, max 10 [default: latest epoch only]"), diff --git a/validator/src/cli.rs b/validator/src/cli.rs index 0d8da15694..50f2b856ee 100644 --- a/validator/src/cli.rs +++ b/validator/src/cli.rs @@ -964,7 +964,7 @@ pub fn app<'a>(version: &'a str, default_args: &'a DefaultArgs) -> App<'a, 'a> { .value_name("MILLISECS") .hidden(true) .takes_value(true) - .validator(|s| is_within_range(s, 1, MAX_BATCH_SEND_RATE_MS)) + .validator(|s| is_within_range(s, 1..=MAX_BATCH_SEND_RATE_MS)) .default_value(&default_args.rpc_send_transaction_batch_ms) .help("The rate at which transactions sent via rpc service are sent in batch."), ) @@ -1000,7 +1000,7 @@ pub fn app<'a>(version: &'a str, default_args: &'a DefaultArgs) -> App<'a, 'a> { .value_name("NUMBER") .hidden(true) .takes_value(true) - .validator(|s| is_within_range(s, 1, MAX_TRANSACTION_BATCH_SIZE)) + .validator(|s| is_within_range(s, 1..=MAX_TRANSACTION_BATCH_SIZE)) .default_value(&default_args.rpc_send_transaction_batch_size) .help("The size of transactions to be sent in batch."), )