Skip to content

Commit a12d632

Browse files
Remove AsRef generics to reduce compilation time & binary size
Replaces all generic `AsRef<X>` uses with `&X` direct references. Includes all instances of a generic parameter of the form `Y: AsRef<X>` and `impl AsRef<X>`. Notably, this included many `AsRef<str>` and `AsRef<Path>`, which are now replaced with `&str` and `&Path`, respectively. This cuts down on compilation time and binary size considerably. Since Rust monomorphizes generics: it must compile new variants of each function for every different concrete type use. Unlike other traits, the codebase's use of `AsRef` doesn't provide much utility as we nearly never swap-out different things that could be turned into the reference type. The code is not only smaller and faster to compile, it is clearer in what, exactly, it requires and what it exactly operates on.
1 parent 4d8c17a commit a12d632

File tree

259 files changed

+3756
-4327
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

259 files changed

+3756
-4327
lines changed

crates/cli/src/cmd/branch.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,8 @@ impl BranchCmd {
196196
) -> Result<(), OxenError> {
197197
let (scheme, host) = get_scheme_and_host_from_repo(repo)?;
198198

199-
check_remote_version_blocking(scheme.clone(), host.clone()).await?;
200-
check_remote_version(scheme, host).await?;
199+
check_remote_version_blocking(&scheme, &host).await?;
200+
check_remote_version(&scheme, &host).await?;
201201

202202
let remote = repo
203203
.get_remote(remote_name)
@@ -219,7 +219,7 @@ impl BranchCmd {
219219
) -> Result<(), OxenError> {
220220
let (scheme, host) = get_scheme_and_host_from_repo(repo)?;
221221

222-
check_remote_version(scheme, host).await?;
222+
check_remote_version(&scheme, &host).await?;
223223

224224
api::client::branches::delete_remote(repo, remote_name, branch_name).await?;
225225
Ok(())

crates/cli/src/cmd/checkout.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use clap::{Arg, Command};
33
use liboxen::error::OxenError;
44
use liboxen::model::LocalRepository;
55
use liboxen::repositories;
6+
use std::path::Path;
67

78
use crate::cmd::RunCmd;
89
pub const NAME: &str = "checkout";
@@ -96,12 +97,12 @@ impl CheckoutCmd {
9697
repo: &LocalRepository,
9798
path: &str,
9899
) -> Result<(), OxenError> {
99-
repositories::checkout::checkout_theirs(repo, path).await?;
100+
repositories::checkout::checkout_theirs(repo, Path::new(path)).await?;
100101
Ok(())
101102
}
102103

103104
pub async fn checkout_ours(&self, repo: &LocalRepository, path: &str) -> Result<(), OxenError> {
104-
repositories::checkout::checkout_ours(repo, path).await?;
105+
repositories::checkout::checkout_ours(repo, Path::new(path)).await?;
105106
Ok(())
106107
}
107108

crates/cli/src/cmd/clone.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ impl RunCmd for CloneCmd {
147147
StorageOpts::default()
148148
}
149149
unsupported_backend => {
150-
return Err(OxenError::basic_str(format!(
150+
return Err(OxenError::basic_str(&format!(
151151
"Unsupported async storage type: {unsupported_backend}"
152152
)));
153153
}
@@ -171,8 +171,8 @@ impl RunCmd for CloneCmd {
171171
let (scheme, host) = api::client::get_scheme_and_host_from_url(&opts.url)?;
172172

173173
// TODO: Do I need to worry about this for remote repo?
174-
check_remote_version_blocking(scheme.clone(), host.clone()).await?;
175-
check_remote_version(scheme, host).await?;
174+
check_remote_version_blocking(&scheme, &host).await?;
175+
check_remote_version(&scheme, &host).await?;
176176

177177
repositories::clone(&opts).await?;
178178

crates/cli/src/cmd/commit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ fn get_message_from_editor(maybe_config: Option<&UserConfig>) -> Result<String,
134134
.status()?;
135135

136136
if !status.success() {
137-
return Err(OxenError::basic_str(format!(
137+
return Err(OxenError::basic_str(&format!(
138138
"Editor '{editor}' exited with non-zero status."
139139
)));
140140
}

crates/cli/src/cmd/db/count.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ impl RunCmd for DbCountCmd {
3030
return Err(OxenError::basic_str("Must supply path"));
3131
};
3232

33-
let count = command::db::count(PathBuf::from(path))?;
33+
let count = command::db::count(&PathBuf::from(path))?;
3434

3535
println!("There are {count} entries in the database");
3636

crates/cli/src/cmd/db/get.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ impl RunCmd for DbGetCmd {
3838
};
3939

4040
let dtype = args.get_one::<String>("dtype").map(|x| x.as_str());
41-
let value = command::db::get(path, key, dtype)?;
41+
let value = command::db::get(std::path::Path::new(path), key, dtype)?;
4242
println!("{value}");
4343

4444
Ok(())

crates/cli/src/cmd/db/list.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ impl RunCmd for DbListCmd {
4040
.get_one::<String>("limit")
4141
.map(|x| x.parse::<usize>().expect("limit must be valid size"));
4242

43-
command::db::list(PathBuf::from(path), limit)?;
43+
command::db::list(&PathBuf::from(path), limit)?;
4444

4545
Ok(())
4646
}

crates/cli/src/cmd/delete_remote.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ impl RunCmd for DeleteRemoteCmd {
8888
return Ok(());
8989
}
9090
Err(e) => {
91-
return Err(OxenError::basic_str(format!(
91+
return Err(OxenError::basic_str(&format!(
9292
"Error confirming deletion: {e}"
9393
)));
9494
}

crates/cli/src/cmd/df.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::path::PathBuf;
1+
use std::path::{Path, PathBuf};
22

33
use async_trait::async_trait;
44
use clap::{Arg, ArgMatches, Command, arg};
@@ -255,13 +255,13 @@ impl RunCmd for DFCmd {
255255

256256
if let Some(revision) = args.get_one::<String>("revision") {
257257
let repo = LocalRepository::from_current_dir()?;
258-
command::df::df_revision(&repo, path, revision, opts).await?;
258+
command::df::df_revision(&repo, Path::new(path), revision, opts).await?;
259259
} else if args.get_flag("schema") || args.get_flag("schema-flat") {
260260
let flatten = args.get_flag("schema-flat");
261-
let result = command::df::schema(path, flatten, opts)?;
261+
let result = command::df::schema(Path::new(path), flatten, opts)?;
262262
println!("{result}");
263263
} else {
264-
command::df(path, opts).await?;
264+
command::df(Path::new(path), opts).await?;
265265
}
266266

267267
Ok(())

crates/cli/src/cmd/diff.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ pub struct DiffCmd;
2121

2222
fn write_to_pager(output: &mut Pager, text: &str) -> Result<(), OxenError> {
2323
write!(output, "{text}")
24-
.map_err(|e| OxenError::basic_str(format!("Could not write to pager: {e}")))
24+
.map_err(|e| OxenError::basic_str(&format!("Could not write to pager: {e}")))
2525
}
2626

2727
fn writeln_to_pager(output: &mut Pager, text: &str) -> Result<(), OxenError> {
2828
writeln!(output, "{text}")
29-
.map_err(|e| OxenError::basic_str(format!("Could not write to pager: {e}")))
29+
.map_err(|e| OxenError::basic_str(&format!("Could not write to pager: {e}")))
3030
}
3131

3232
#[async_trait]
@@ -339,7 +339,7 @@ impl DiffCmd {
339339
match result {
340340
DiffResult::Tabular(result) => {
341341
let mut df = result.contents.clone();
342-
tabular::write_df(&mut df, file_path.clone())?;
342+
tabular::write_df(&mut df, &file_path.clone())?;
343343
}
344344
DiffResult::Text(_) => {
345345
println!("Saving to disk not supported for text output");

0 commit comments

Comments
 (0)