Optimize: Create new fn helpers::cargo_home

that caches return value of `home::cargo_home`

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
Jiahao XU 2022-07-21 13:13:07 +10:00
parent 305a4e4c30
commit 21eac33e1f
No known key found for this signature in database
GPG key ID: 591C0B03040416D6
3 changed files with 14 additions and 2 deletions

View file

@ -1,12 +1,14 @@
use std::fmt::Debug;
use std::fs;
use std::io;
use std::ops;
use std::path::{Path, PathBuf};
use bytes::Bytes;
use cargo_toml::Manifest;
use futures_util::stream::Stream;
use log::debug;
use once_cell::sync::OnceCell;
use reqwest::{tls, Client, ClientBuilder, Method, Response};
use serde::Serialize;
use tempfile::NamedTempFile;
@ -40,6 +42,14 @@ pub use tls_version::TLSVersion;
mod crate_name;
pub use crate_name::CrateName;
pub fn cargo_home() -> Result<&'static Path, io::Error> {
static CARGO_HOME: OnceCell<PathBuf> = OnceCell::new();
CARGO_HOME
.get_or_try_init(home::cargo_home)
.map(ops::Deref::deref)
}
pub async fn await_task<T>(task: tokio::task::JoinHandle<miette::Result<T>>) -> miette::Result<T> {
match task.await {
Ok(res) => res,

View file

@ -10,6 +10,7 @@ use serde::{Deserialize, Serialize};
use thiserror::Error;
use super::CrateVersionSource;
use crate::cargo_home;
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct CratesToml {
@ -18,7 +19,7 @@ pub struct CratesToml {
impl CratesToml {
pub fn default_path() -> Result<PathBuf, CratesTomlParseError> {
Ok(home::cargo_home()?.join(".crates.toml"))
Ok(cargo_home()?.join(".crates.toml"))
}
pub fn load() -> Result<Self, CratesTomlParseError> {

View file

@ -9,6 +9,7 @@ use serde::{Deserialize, Serialize};
use thiserror::Error;
use super::CrateVersionSource;
use crate::cargo_home;
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct Crates2Json {
@ -39,7 +40,7 @@ pub struct CrateInfo {
impl Crates2Json {
pub fn default_path() -> Result<PathBuf, Crates2JsonParseError> {
Ok(home::cargo_home()?.join(".crates2.json"))
Ok(cargo_home()?.join(".crates2.json"))
}
pub fn load() -> Result<Self, Crates2JsonParseError> {