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

Use bool::then instead of then_some with function calls #6156

Merged
merged 6 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions cumulus/polkadot-omni-node/lib/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,13 +266,15 @@ pub fn run<CliConfig: crate::cli::CliConfig>(cmd_config: RunConfig) -> Result<()
}

let hwbench = (!cli.no_hardware_benchmarks)
.then_some(config.database.path().map(|database_path| {
let _ = std::fs::create_dir_all(database_path);
sc_sysinfo::gather_hwbench(
Some(database_path),
&SUBSTRATE_REFERENCE_HARDWARE,
)
}))
.then(|| {
config.database.path().map(|database_path| {
let _ = std::fs::create_dir_all(database_path);
sc_sysinfo::gather_hwbench(
Some(database_path),
&SUBSTRATE_REFERENCE_HARDWARE,
)
})
})
.flatten();

let parachain_account =
Expand Down
10 changes: 6 additions & 4 deletions polkadot/cli/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,10 +203,12 @@ where

runner.run_node_until_exit(move |config| async move {
let hwbench = (!cli.run.no_hardware_benchmarks)
.then_some(config.database.path().map(|database_path| {
let _ = std::fs::create_dir_all(&database_path);
sc_sysinfo::gather_hwbench(Some(database_path), &SUBSTRATE_REFERENCE_HARDWARE)
}))
.then(|| {
config.database.path().map(|database_path| {
let _ = std::fs::create_dir_all(&database_path);
sc_sysinfo::gather_hwbench(Some(database_path), &SUBSTRATE_REFERENCE_HARDWARE)
})
})
.flatten();

let database_source = config.database.clone();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ impl ValidatorGroupsBuffer {
.validators
.iter()
.enumerate()
.filter_map(|(idx, authority_id)| bits[idx].then_some(authority_id.clone()))
.filter_map(|(idx, authority_id)| bits[idx].then(|| authority_id.clone()))
.collect();

if let Some(last_group) = self.group_infos.iter().last() {
Expand Down
10 changes: 6 additions & 4 deletions substrate/bin/node/cli/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,10 +419,12 @@ pub fn new_full_base<N: NetworkBackend<Block, <Block as BlockT>::Hash>>(
let enable_offchain_worker = config.offchain_worker.enabled;

let hwbench = (!disable_hardware_benchmarks)
.then_some(config.database.path().map(|database_path| {
let _ = std::fs::create_dir_all(&database_path);
sc_sysinfo::gather_hwbench(Some(database_path), &SUBSTRATE_REFERENCE_HARDWARE)
}))
.then(|| {
config.database.path().map(|database_path| {
let _ = std::fs::create_dir_all(&database_path);
sc_sysinfo::gather_hwbench(Some(database_path), &SUBSTRATE_REFERENCE_HARDWARE)
})
})
.flatten();

let sc_service::PartialComponents {
Expand Down
2 changes: 1 addition & 1 deletion substrate/client/network/src/discovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,7 @@ impl NetworkBehaviour for DiscoveryBehaviour {
let mut list: LinkedHashSet<_> = self
.permanent_addresses
.iter()
.filter_map(|(p, a)| (*p == peer_id).then_some(a.clone()))
.filter_map(|(p, a)| (*p == peer_id).then(|| a.clone()))
.collect();

if let Some(ephemeral_addresses) = self.ephemeral_addresses.get(&peer_id) {
Expand Down
2 changes: 1 addition & 1 deletion substrate/client/network/sync/src/strategy/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ impl<B: BlockT> StateStrategy<B> {
peer_id: PeerId,
announce: &BlockAnnounce<B::Header>,
) -> Option<(B::Hash, NumberFor<B>)> {
is_best.then_some({
is_best.then(|| {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one is pretty bad, it makes on_validated_block_announce behave as if is_best was always true

let best_number = *announce.header.number();
let best_hash = announce.header.hash();
if let Some(ref mut peer) = self.peers.get_mut(&peer_id) {
Expand Down
2 changes: 1 addition & 1 deletion substrate/client/network/sync/src/strategy/warp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ where
peer_id: PeerId,
announce: &BlockAnnounce<B::Header>,
) -> Option<(B::Hash, NumberFor<B>)> {
is_best.then_some({
is_best.then(|| {
let best_number = *announce.header.number();
let best_hash = announce.header.hash();
if let Some(ref mut peer) = self.peers.get_mut(&peer_id) {
Expand Down
2 changes: 1 addition & 1 deletion substrate/frame/contracts/proc-macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ fn expand_docs(def: &EnvDef) -> TokenStream2 {
/// `expand_impls()`).
fn expand_env(def: &EnvDef, docs: bool) -> TokenStream2 {
let impls = expand_impls(def);
let docs = docs.then_some(expand_docs(def)).unwrap_or(TokenStream2::new());
let docs = docs.then(|| expand_docs(def)).unwrap_or(TokenStream2::new());
let stable_api_count = def.host_funcs.iter().filter(|f| f.is_stable).count();

quote! {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ pub fn expand_pallet_struct(def: &mut Def) -> proc_macro2::TokenStream {
let whitelisted_storage_idents: Vec<syn::Ident> = def
.storages
.iter()
.filter_map(|s| s.whitelisted.then_some(s.ident.clone()))
.filter_map(|s| s.whitelisted.then(|| s.ident.clone()))
.collect();

let whitelisted_storage_keys_impl = quote::quote![
Expand Down
4 changes: 2 additions & 2 deletions substrate/frame/support/src/migrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -825,14 +825,14 @@ impl<T: SteppedMigration> SteppedMigrations for T {

fn nth_id(n: u32) -> Option<Vec<u8>> {
n.is_zero()
.then_some(T::id().encode())
.then(|| T::id().encode())
.defensive_proof("nth_id should only be called with n==0")
}

fn nth_max_steps(n: u32) -> Option<Option<u32>> {
// It should be generally fine to call with n>0, but the code should not attempt to.
n.is_zero()
.then_some(T::max_steps())
.then(|| T::max_steps())
.defensive_proof("nth_max_steps should only be called with n==0")
}

Expand Down
16 changes: 9 additions & 7 deletions templates/parachain/node/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,13 +220,15 @@ pub fn run() -> Result<()> {

runner.run_node_until_exit(|config| async move {
let hwbench = (!cli.no_hardware_benchmarks)
.then_some(config.database.path().map(|database_path| {
let _ = std::fs::create_dir_all(database_path);
sc_sysinfo::gather_hwbench(
Some(database_path),
&SUBSTRATE_REFERENCE_HARDWARE,
)
}))
.then(|| {
config.database.path().map(|database_path| {
let _ = std::fs::create_dir_all(database_path);
sc_sysinfo::gather_hwbench(
Some(database_path),
&SUBSTRATE_REFERENCE_HARDWARE,
)
})
})
.flatten();

let para_id = chain_spec::Extensions::try_get(&*config.chain_spec)
Expand Down
Loading