Skip to content

Commit 0047841

Browse files
Replace impl AsRef<T> with &T (pgcentralfoundation#2182)
A pattern that was popular some years ago, when `impl Trait` arguments were first introduced, was to make everything an `impl Trait` arg. This can be undesirable because it weakens type inference and spreads whatever indirection is used throughout the codebase, demanding repeat invocations of `.as_ref()` or whatever. Unfortunately, pgrx was first made in the period of that being en vogue. The alternative, of requiring things like `Option<&T>`, does demand using `Option::as_deref` a bit, but fortunately that convenience exists nowadays. It also means that the indirection is handled once at the top level and not addressed after that, which is more appropriate for internals. ~~None~~ **Almost** none of the relevant functions in pgrx are considered public API, after all, as they are found throughout cargo-pgrx and pgrx-sql-entity-graph. Remove almost all instances of `impl AsRef`, usually with `Path` or `str`, and replace them with `&Path` or `&str` as appropriate. This deletes a few lines from the codebase outright, and in those that remain, it usually simplifies the existing code. In a couple cases, the benefit involves actually removing `clone`s of data! The remaining instances linger with a note about their shame.
1 parent 83925a7 commit 0047841

15 files changed

Lines changed: 130 additions & 136 deletions

File tree

cargo-pgrx/src/command/connect.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ impl CommandExecute for Connect {
5151
let (package_manifest, package_manifest_path) = get_package_manifest(
5252
&Features::default(),
5353
self.package.as_ref(),
54-
self.manifest_path.as_ref(),
54+
self.manifest_path.as_deref(),
5555
)?;
5656
let (pg_config, _pg_version) = match pg_config_and_version(
5757
&pgrx,
@@ -75,7 +75,7 @@ impl CommandExecute for Connect {
7575
Some(dbname) => dbname,
7676
None => {
7777
// We should infer from package
78-
get_property(package_manifest_path, "extname")
78+
get_property(&package_manifest_path, "extname")
7979
.wrap_err("could not determine extension name")?
8080
.ok_or(eyre!("extname not found in control file"))?
8181
}

cargo-pgrx/src/command/get.rs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,15 @@ pub(crate) struct Get {
3333
impl CommandExecute for Get {
3434
#[tracing::instrument(level = "error", skip(self))]
3535
fn execute(self) -> eyre::Result<()> {
36-
let metadata = crate::metadata::metadata(&Default::default(), self.manifest_path.as_ref())
37-
.wrap_err("couldn't get cargo metadata")?;
38-
crate::metadata::validate(self.manifest_path.as_ref(), &metadata)?;
36+
let metadata =
37+
crate::metadata::metadata(&Default::default(), self.manifest_path.as_deref())
38+
.wrap_err("couldn't get cargo metadata")?;
39+
crate::metadata::validate(self.manifest_path.as_deref(), &metadata)?;
3940
let package_manifest_path =
4041
crate::manifest::manifest_path(&metadata, self.package.as_ref())
4142
.wrap_err("Couldn't get manifest path")?;
4243

43-
if let Some(value) = get_property(package_manifest_path, &self.name)? {
44+
if let Some(value) = get_property(&package_manifest_path, &self.name)? {
4445
println!("{value}");
4546
}
4647
Ok(())
@@ -49,9 +50,9 @@ impl CommandExecute for Get {
4950

5051
#[tracing::instrument(level = "error", skip_all, fields(
5152
%name,
52-
manifest_path = %manifest_path.as_ref().display(),
53+
manifest_path = %manifest_path.display(),
5354
))]
54-
pub fn get_property(manifest_path: impl AsRef<Path>, name: &str) -> eyre::Result<Option<String>> {
55+
pub fn get_property(manifest_path: &Path, name: &str) -> eyre::Result<Option<String>> {
5556
let (control_file, extname) = find_control_file(manifest_path)?;
5657

5758
if name == "extname" {
@@ -84,13 +85,10 @@ pub fn get_property(manifest_path: impl AsRef<Path>, name: &str) -> eyre::Result
8485
Ok(None)
8586
}
8687

87-
pub(crate) fn find_control_file(
88-
manifest_path: impl AsRef<Path>,
89-
) -> eyre::Result<(PathBuf, String)> {
88+
pub(crate) fn find_control_file(manifest_path: &Path) -> eyre::Result<(PathBuf, String)> {
9089
let parent = manifest_path
91-
.as_ref()
9290
.parent()
93-
.ok_or_else(|| eyre!("could not get parent of `{}`", manifest_path.as_ref().display()))?;
91+
.ok_or_else(|| eyre!("could not get parent of `{}`", manifest_path.display()))?;
9492

9593
for f in (std::fs::read_dir(parent).wrap_err_with(|| {
9694
eyre!("cannot open current directory `{}` for reading", parent.display())
@@ -112,7 +110,7 @@ pub(crate) fn find_control_file(
112110
}
113111
}
114112

115-
Err(eyre!("control file not found in `{}`", manifest_path.as_ref().display()))
113+
Err(eyre!("control file not found in `{}`", manifest_path.display()))
116114
}
117115

118116
fn determine_git_hash() -> eyre::Result<Option<String>> {

cargo-pgrx/src/command/install.rs

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,9 @@ impl CommandExecute for Install {
7171
return sudo_install.execute();
7272
}
7373

74-
let metadata = crate::metadata::metadata(&self.features, self.manifest_path.as_ref())
74+
let metadata = crate::metadata::metadata(&self.features, self.manifest_path.as_deref())
7575
.wrap_err("couldn't get cargo metadata")?;
76-
crate::metadata::validate(self.manifest_path.as_ref(), &metadata)?;
76+
crate::metadata::validate(self.manifest_path.as_deref(), &metadata)?;
7777
let package_manifest_path =
7878
crate::manifest::manifest_path(&metadata, self.package.as_ref())
7979
.wrap_err("Couldn't get manifest path")?;
@@ -100,7 +100,7 @@ impl CommandExecute for Install {
100100

101101
display_version_info(&pg_config, &PgVersionSource::PgConfig(pg_config.label()?));
102102
install_extension(
103-
self.manifest_path.as_ref(),
103+
self.manifest_path.as_deref(),
104104
self.package.as_ref(),
105105
&package_manifest_path,
106106
&pg_config,
@@ -122,7 +122,7 @@ impl CommandExecute for Install {
122122
features = ?features.features,
123123
))]
124124
pub(crate) fn install_extension(
125-
user_manifest_path: Option<impl AsRef<Path>>,
125+
user_manifest_path: Option<&Path>,
126126
user_package: Option<&String>,
127127
package_manifest_path: &Path,
128128
pg_config: &PgConfig,
@@ -140,7 +140,7 @@ pub(crate) fn install_extension(
140140
let versioned_so = get_property(package_manifest_path, "module_pathname")?.is_none();
141141

142142
let build_command_output =
143-
build_extension(user_manifest_path.as_ref(), user_package, profile, features, target)?;
143+
build_extension(user_manifest_path, user_package, profile, features, target)?;
144144
let build_command_bytes = build_command_output.stdout;
145145
let build_command_reader = BufReader::new(build_command_bytes.as_slice());
146146
let build_command_stream = CargoMessage::parse_stream(build_command_reader);
@@ -241,7 +241,7 @@ fn copy_file(
241241
dest: PathBuf,
242242
msg: &str,
243243
do_filter: bool,
244-
package_manifest_path: impl AsRef<Path>,
244+
package_manifest_path: &Path,
245245
output_tracking: &mut Vec<PathBuf>,
246246
) -> eyre::Result<()> {
247247
let Some(dest_dir) = dest.parent() else {
@@ -281,7 +281,7 @@ fn copy_file(
281281
}
282282

283283
pub(crate) fn build_extension(
284-
user_manifest_path: Option<impl AsRef<Path>>,
284+
user_manifest_path: Option<&Path>,
285285
user_package: Option<&String>,
286286
profile: &CargoProfile,
287287
features: &clap_cargo::Features,
@@ -295,7 +295,7 @@ pub(crate) fn build_extension(
295295

296296
if let Some(user_manifest_path) = user_manifest_path {
297297
command.arg("--manifest-path");
298-
command.arg(user_manifest_path.as_ref());
298+
command.arg(user_manifest_path);
299299
}
300300

301301
if let Some(user_package) = user_package {
@@ -344,9 +344,9 @@ pub(crate) fn build_extension(
344344
}
345345

346346
fn copy_sql_files(
347-
user_manifest_path: Option<impl AsRef<Path>>,
347+
user_manifest_path: Option<&Path>,
348348
user_package: Option<&String>,
349-
package_manifest_path: impl AsRef<Path>,
349+
package_manifest_path: &Path,
350350
profile: &CargoProfile,
351351
is_test: bool,
352352
features: &clap_cargo::Features,
@@ -364,21 +364,21 @@ fn copy_sql_files(
364364
crate::command::schema::generate_schema(
365365
user_manifest_path,
366366
user_package,
367-
&package_manifest_path,
367+
package_manifest_path.as_ref(),
368368
profile,
369369
is_test,
370370
features,
371371
target,
372372
Some(&dest),
373-
Option::<String>::None,
373+
None,
374374
None,
375375
skip_build,
376376
output_tracking,
377377
)?;
378378
}
379379

380380
// now copy all the version upgrade files too
381-
if let Ok(dir) = fs::read_dir(package_manifest_path.as_ref().parent().unwrap().join("sql/")) {
381+
if let Ok(dir) = fs::read_dir(package_manifest_path.parent().unwrap().join("sql/")) {
382382
for sql in dir.flatten() {
383383
let filename = sql.file_name().into_string().unwrap();
384384

@@ -444,8 +444,8 @@ pub(crate) fn find_library_file(
444444

445445
static CARGO_VERSION: OnceLock<MemoizeKeyValue> = OnceLock::new();
446446

447-
pub(crate) fn get_version(manifest_path: impl AsRef<Path>) -> eyre::Result<String> {
448-
let path_string = manifest_path.as_ref().to_owned();
447+
pub(crate) fn get_version(manifest_path: &Path) -> eyre::Result<String> {
448+
let path_string = manifest_path.to_owned();
449449

450450
if let Some(version) =
451451
CARGO_VERSION.get_or_init(Default::default).lock().unwrap().get(&path_string)
@@ -486,8 +486,8 @@ pub(crate) fn get_version(manifest_path: impl AsRef<Path>) -> eyre::Result<Strin
486486

487487
static GIT_HASH: OnceLock<MemoizeKeyValue> = OnceLock::new();
488488

489-
fn get_git_hash(manifest_path: impl AsRef<Path>) -> eyre::Result<String> {
490-
let path_string = manifest_path.as_ref().to_owned();
489+
fn get_git_hash(manifest_path: &Path) -> eyre::Result<String> {
490+
let path_string = manifest_path.to_owned();
491491

492492
let mut mutex = GIT_HASH.get_or_init(Default::default).lock().unwrap();
493493
if let Some(hash) = mutex.get(&path_string) {
@@ -540,8 +540,7 @@ fn make_relative_extdir(_: PathBuf) -> PathBuf {
540540
"share/extension".into()
541541
}
542542

543-
pub(crate) fn format_display_path(path: impl AsRef<Path>) -> eyre::Result<String> {
544-
let path = path.as_ref();
543+
pub(crate) fn format_display_path(path: &Path) -> eyre::Result<String> {
545544
let out = path
546545
.strip_prefix(get_target_dir()?.parent().unwrap())
547546
.unwrap_or(path)
@@ -550,7 +549,7 @@ pub(crate) fn format_display_path(path: impl AsRef<Path>) -> eyre::Result<String
550549
Ok(out)
551550
}
552551

553-
fn filter_contents(manifest_path: impl AsRef<Path>, mut input: String) -> eyre::Result<String> {
552+
fn filter_contents(manifest_path: &Path, mut input: String) -> eyre::Result<String> {
554553
if input.contains("@GIT_HASH@") {
555554
// avoid doing this if we don't actually have the token
556555
// the project might not be a git repo so running `git`

cargo-pgrx/src/command/package.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ pub(crate) struct Package {
5151

5252
impl Package {
5353
pub(crate) fn perform(mut self) -> eyre::Result<(PathBuf, Vec<PathBuf>)> {
54-
let metadata = crate::metadata::metadata(&self.features, self.manifest_path.as_ref())
54+
let metadata = crate::metadata::metadata(&self.features, self.manifest_path.as_deref())
5555
.wrap_err("couldn't get cargo metadata")?;
56-
crate::metadata::validate(self.manifest_path.as_ref(), &metadata)?;
56+
crate::metadata::validate(self.manifest_path.as_deref(), &metadata)?;
5757
let package_manifest_path =
5858
crate::manifest::manifest_path(&metadata, self.package.as_ref())
5959
.wrap_err("Couldn't get manifest path")?;
@@ -85,7 +85,7 @@ impl Package {
8585
};
8686

8787
let output_files = package_extension(
88-
self.manifest_path.as_ref(),
88+
self.manifest_path.as_deref(),
8989
self.package.as_ref(),
9090
&package_manifest_path,
9191
&pg_config,
@@ -114,7 +114,7 @@ impl CommandExecute for Package {
114114
test = is_test,
115115
))]
116116
pub(crate) fn package_extension(
117-
user_manifest_path: Option<impl AsRef<Path>>,
117+
user_manifest_path: Option<&Path>,
118118
user_package: Option<&String>,
119119
package_manifest_path: &Path,
120120
pg_config: &PgConfig,
@@ -147,7 +147,7 @@ pub(crate) fn package_extension(
147147

148148
pub(crate) fn build_base_path(
149149
pg_config: &PgConfig,
150-
manifest_path: impl AsRef<Path>,
150+
manifest_path: &Path,
151151
profile: &CargoProfile,
152152
target: Option<&str>,
153153
) -> eyre::Result<PathBuf> {

0 commit comments

Comments
 (0)