Skip to content

Commit bc370e3

Browse files
feat: reduce the amount of heck-related allocations (#4634)
Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
1 parent d3e19e3 commit bc370e3

File tree

5 files changed

+26
-23
lines changed

5 files changed

+26
-23
lines changed

.changes/heck-allocations.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
"tauri-build": patch
3+
"tauri": patch
4+
"tauri-bundler": patch
5+
"cli.rs": patch
6+
"cli.js": patch
7+
---
8+
9+
Reduce the amount of allocations when converting cases.

core/tauri-build/src/lib.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
#![cfg_attr(doc_cfg, feature(doc_cfg))]
66

77
pub use anyhow::Result;
8-
use heck::ToSnakeCase;
8+
use heck::AsShoutySnakeCase;
9+
910
use tauri_utils::resources::{external_binaries, resource_relpath, ResourcePaths};
1011

1112
use std::path::{Path, PathBuf};
@@ -68,12 +69,9 @@ fn copy_resources(resources: ResourcePaths<'_>, path: &Path) -> Result<()> {
6869
fn has_feature(feature: &str) -> bool {
6970
// when a feature is enabled, Cargo sets the `CARGO_FEATURE_<name` env var to 1
7071
// https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-build-scripts
71-
std::env::var(format!(
72-
"CARGO_FEATURE_{}",
73-
feature.to_snake_case().to_uppercase()
74-
))
75-
.map(|x| x == "1")
76-
.unwrap_or(false)
72+
std::env::var(format!("CARGO_FEATURE_{}", AsShoutySnakeCase(feature)))
73+
.map(|x| x == "1")
74+
.unwrap_or(false)
7775
}
7876

7977
// creates a cfg alias if `has_feature` is true.

core/tauri/build.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
// SPDX-License-Identifier: Apache-2.0
33
// SPDX-License-Identifier: MIT
44

5+
use heck::AsShoutySnakeCase;
6+
use heck::AsSnakeCase;
57
use heck::ToSnakeCase;
8+
69
use once_cell::sync::OnceCell;
710

811
use std::{path::Path, sync::Mutex};
@@ -19,12 +22,9 @@ fn has_feature(feature: &str) -> bool {
1922

2023
// when a feature is enabled, Cargo sets the `CARGO_FEATURE_<name` env var to 1
2124
// https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-build-scripts
22-
std::env::var(format!(
23-
"CARGO_FEATURE_{}",
24-
feature.to_snake_case().to_uppercase()
25-
))
26-
.map(|x| x == "1")
27-
.unwrap_or(false)
25+
std::env::var(format!("CARGO_FEATURE_{}", AsShoutySnakeCase(feature)))
26+
.map(|x| x == "1")
27+
.unwrap_or(false)
2828
}
2929

3030
// creates a cfg alias if `has_feature` is true.
@@ -148,11 +148,11 @@ fn alias_module(module: &str, apis: &[&str], api_all: bool) {
148148
for api in apis {
149149
let has = has_feature(&format!("{}-{}", module, api)) || all;
150150
alias(
151-
&format!("{}_{}", module.to_snake_case(), api.to_snake_case()),
151+
&format!("{}_{}", AsSnakeCase(module), AsSnakeCase(api)),
152152
has,
153153
);
154154
any = any || has;
155155
}
156156

157-
alias(&format!("{}_any", module.to_snake_case()), any);
157+
alias(&format!("{}_any", AsSnakeCase(module)), any);
158158
}

tooling/bundler/src/bundle/linux/debian.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
use super::super::common;
2626
use crate::Settings;
2727
use anyhow::Context;
28-
use heck::ToKebabCase;
28+
use heck::AsKebabCase;
2929
use image::{self, codecs::png::PngDecoder, ImageDecoder};
3030
use libflate::gzip;
3131
use log::info;
@@ -170,11 +170,7 @@ fn generate_control_file(
170170
// https://www.debian.org/doc/debian-policy/ch-controlfields.html
171171
let dest_path = control_dir.join("control");
172172
let mut file = common::create_file(&dest_path)?;
173-
writeln!(
174-
file,
175-
"Package: {}",
176-
settings.product_name().to_kebab_case().to_ascii_lowercase()
177-
)?;
173+
writeln!(file, "Package: {}", AsKebabCase(settings.product_name()))?;
178174
writeln!(file, "Version: {}", settings.version_string())?;
179175
writeln!(file, "Architecture: {}", arch)?;
180176
// Installed-Size must be divided by 1024, see https://www.debian.org/doc/debian-policy/ch-controlfields.html#installed-size

tooling/cli/src/plugin/init.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::{
1010
use anyhow::Context;
1111
use clap::Parser;
1212
use handlebars::{to_json, Handlebars};
13-
use heck::{ToKebabCase, ToSnakeCase};
13+
use heck::{AsKebabCase, ToKebabCase, ToSnakeCase};
1414
use include_dir::{include_dir, Dir};
1515
use log::warn;
1616
use std::{collections::BTreeMap, env::current_dir, fs::remove_dir_all, path::PathBuf};
@@ -58,7 +58,7 @@ pub fn command(mut options: Options) -> Result<()> {
5858
options.load();
5959
let template_target_path = PathBuf::from(options.directory).join(&format!(
6060
"tauri-plugin-{}",
61-
options.plugin_name.to_kebab_case()
61+
AsKebabCase(&options.plugin_name)
6262
));
6363
let metadata = serde_json::from_str::<VersionMetadata>(include_str!("../../metadata.json"))?;
6464
if template_target_path.exists() {

0 commit comments

Comments
 (0)