Skip to content

Commit

Permalink
[fix,perf]: attempt to make FileProvider use a reqwest client reference
Browse files Browse the repository at this point in the history
  • Loading branch information
null8626 committed Feb 2, 2024
1 parent bbdaaa9 commit 9f753cd
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
6 changes: 3 additions & 3 deletions src/app/from_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ impl App {

let version = self.select(
"Select a version",
versions
&versions
.iter()
.map(|v| {
SelectItem(
Expand Down Expand Up @@ -126,7 +126,7 @@ impl App {

let version = self.select(
"Select a version",
versions
&versions
.iter()
.map(|v| {
SelectItem(
Expand Down Expand Up @@ -165,7 +165,7 @@ impl App {

let version = self.select(
"Select a version",
versions
&versions
.iter()
.map(|v| {
SelectItem(
Expand Down
8 changes: 4 additions & 4 deletions src/commands/init/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ pub async fn run(base_app: BaseApp, args: Args) -> Result<()> {
};

// toml checks and init
if let InitType::Network = ty {
if matches!(ty, InitType::Network) {
if let Some(_nw) = Network::load()? {
bail!("network.toml already exists");
}
Expand Down Expand Up @@ -193,7 +193,7 @@ pub async fn run(base_app: BaseApp, args: Args) -> Result<()> {
.await?;
}
InitType::Packwiz(src) => {
app.packwiz().import_from_source(src.clone()).await?;
app.packwiz().import_from_source(src).await?;
}
}

Expand Down Expand Up @@ -229,7 +229,7 @@ pub async fn run(base_app: BaseApp, args: Args) -> Result<()> {
}

//env
if let InitType::Network = ty {
if matches!(ty, InitType::Network) {
fs::create_dir_all("./servers")?;
} else {
fs::create_dir_all("./config")?;
Expand Down Expand Up @@ -264,7 +264,7 @@ pub async fn run(base_app: BaseApp, args: Args) -> Result<()> {

initialize_environment()?;

if let InitType::Network = ty {
if matches!(ty, InitType::Network) {
println!(
" > {} {} {}\n > {}\n {}\n {}\n {}",
style("Network").green(),
Expand Down
16 changes: 9 additions & 7 deletions src/interop/packwiz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ use crate::{
};

#[derive(Debug, Clone)]
pub enum FileProvider {
pub enum FileProvider<'a> {
LocalFolder(PathBuf),
RemoteURL(reqwest::Client, reqwest::Url),
RemoteURL(&'a reqwest::Client, reqwest::Url),
}

impl FileProvider {
impl FileProvider<'_> {
pub async fn parse_toml<T: DeserializeOwned>(&self, path: &str) -> Result<T> {
match self {
Self::LocalFolder(folder) => {
Expand All @@ -51,19 +51,21 @@ impl FileProvider {
pub struct PackwizInterop<'a>(pub &'a mut App);

impl<'a> PackwizInterop<'a> {
pub fn get_file_provider(&self, s: &str) -> Result<FileProvider> {
pub fn get_file_provider(&'a self, s: &str) -> Result<FileProvider<'a>> {
Ok(if s.starts_with("http") {
FileProvider::RemoteURL(self.0.http_client.clone(), s.try_into()?)
FileProvider::RemoteURL(&self.0.http_client, s.try_into()?)
} else {
FileProvider::LocalFolder(s.into())
})
}

pub async fn import_all(&mut self, from: &str) -> Result<()> {
self.import_from_source(self.get_file_provider(from)?).await
let provider = self.get_file_provider(from)?;

self.import_from_source(&provider).await

Check failure on line 65 in src/interop/packwiz.rs

View workflow job for this annotation

GitHub Actions / clippy

`provider` does not live long enough

error[E0597]: `provider` does not live long enough --> src/interop/packwiz.rs:65:33 | 62 | pub async fn import_all(&mut self, from: &str) -> Result<()> { | --------- lifetime `'1` appears in the type of `self` 63 | let provider = self.get_file_provider(from)?; | -------- binding `provider` declared here 64 | 65 | self.import_from_source(&provider).await | ------------------------^^^^^^^^^- | | | | | borrowed value does not live long enough | argument requires that `provider` is borrowed for `'1` 66 | } | - `provider` dropped here while still borrowed

Check failure on line 65 in src/interop/packwiz.rs

View workflow job for this annotation

GitHub Actions / clippy

cannot borrow `*self` as mutable because it is also borrowed as immutable

error[E0502]: cannot borrow `*self` as mutable because it is also borrowed as immutable --> src/interop/packwiz.rs:65:9 | 62 | pub async fn import_all(&mut self, from: &str) -> Result<()> { | --------- lifetime `'1` appears in the type of `self` 63 | let provider = self.get_file_provider(from)?; | ---------------------------- | | | immutable borrow occurs here | argument requires that `*self` is borrowed for `'1` 64 | 65 | self.import_from_source(&provider).await | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ mutable borrow occurs here
}

pub async fn import_from_source(&mut self, source: FileProvider) -> Result<()> {
pub async fn import_from_source(&'a mut self, source: &'a FileProvider<'a>) -> Result<()> {
let progress_bar = self.0.multi_progress.add(ProgressBar::new_spinner());
progress_bar.set_style(ProgressStyle::with_template("{spinner:.blue} {msg}")?);
progress_bar.set_message("Reading pack.toml...");
Expand Down

0 comments on commit 9f753cd

Please sign in to comment.