From f49d330b6f97b08e72b1a448af0021708b2a2417 Mon Sep 17 00:00:00 2001 From: jdx <216188+jdx@users.noreply.github.com> Date: Fri, 29 Nov 2024 05:00:04 +0000 Subject: [PATCH] refactor: clean up arcs --- src/backend/mod.rs | 26 +++++++++---------- src/toolset/install_state.rs | 49 +++++++++++++++--------------------- 2 files changed, 32 insertions(+), 43 deletions(-) diff --git a/src/backend/mod.rs b/src/backend/mod.rs index 97fc0709ff..bfcc6a5b28 100644 --- a/src/backend/mod.rs +++ b/src/backend/mod.rs @@ -4,7 +4,7 @@ use std::fs::File; use std::hash::Hash; use std::ops::Deref; use std::path::{Path, PathBuf}; -use std::sync::{Arc, Mutex, MutexGuard}; +use std::sync::{Arc, Mutex}; use crate::cache::{CacheManager, CacheManagerBuilder}; use crate::cli::args::{BackendArg, ToolVersionType}; @@ -48,9 +48,8 @@ pub type VersionCacheManager = CacheManager>; static TOOLS: Mutex>> = Mutex::new(None); -fn load_tools() -> MutexGuard<'static, Option>> { - let mut memo_tools = TOOLS.lock().unwrap(); - if memo_tools.is_some() { +fn load_tools() -> Arc { + if let Some(memo_tools) = TOOLS.lock().unwrap().clone() { return memo_tools; } time!("load_tools start"); @@ -79,24 +78,24 @@ fn load_tools() -> MutexGuard<'static, Option>> { .into_iter() .map(|backend| (backend.ba().short.clone(), backend)) .collect(); - *memo_tools = Some(Arc::new(tools.clone())); + let tools = Arc::new(tools); + *TOOLS.lock().unwrap() = Some(tools.clone()); time!("load_tools done"); - memo_tools + tools } pub fn list() -> BackendList { - load_tools().as_ref().unwrap().values().cloned().collect() + load_tools().values().cloned().collect() } pub fn get(ba: &BackendArg) -> Option { - let mut m = load_tools(); - let backends = m.as_mut().unwrap(); + let backends = load_tools(); if let Some(backend) = backends.get(&ba.short) { Some(backend.clone()) } else if let Some(backend) = arg_to_backend(ba.clone()) { - let mut backends = (**backends).clone(); + let mut backends = backends.deref().clone(); backends.insert(ba.short.clone(), backend.clone()); - *m = Some(Arc::new(backends)); + *TOOLS.lock().unwrap() = Some(Arc::new(backends)); Some(backend) } else { None @@ -104,10 +103,9 @@ pub fn get(ba: &BackendArg) -> Option { } pub fn remove(short: &str) { - let mut t = load_tools(); - let mut backends = t.clone().unwrap().deref().clone(); + let mut backends = load_tools().deref().clone(); backends.remove(short); - *t = Some(Arc::new(backends)); + *TOOLS.lock().unwrap() = Some(Arc::new(backends)); } pub fn arg_to_backend(ba: BackendArg) -> Option { diff --git a/src/toolset/install_state.rs b/src/toolset/install_state.rs index 881d698aa6..c3a4c4e110 100644 --- a/src/toolset/install_state.rs +++ b/src/toolset/install_state.rs @@ -10,12 +10,12 @@ use rayon::prelude::*; use std::collections::BTreeMap; use std::ops::Deref; use std::path::PathBuf; -use std::sync::{Arc, Mutex, MutexGuard}; +use std::sync::{Arc, Mutex}; use versions::Versioning; type InstallStatePlugins = BTreeMap; type InstallStateTools = BTreeMap; -type MutexResult = Result>>>; +type MutexResult = Result>; #[derive(Debug, Clone)] pub struct InstallStateTool { @@ -46,12 +46,11 @@ pub(crate) fn init() -> Result<()> { } fn init_plugins() -> MutexResult { - let mut mu = INSTALL_STATE_PLUGINS.lock().unwrap(); - if mu.is_some() { - return Ok(mu); + if let Some(plugins) = INSTALL_STATE_PLUGINS.lock().unwrap().clone() { + return Ok(plugins); } let dirs = file::dir_subdirs(&dirs::PLUGINS)?; - let plugins = dirs + let plugins: InstallStatePlugins = dirs .into_iter() .filter_map(|d| { time!("init_plugins {d}"); @@ -65,14 +64,14 @@ fn init_plugins() -> MutexResult { } }) .collect(); - *mu = Some(Arc::new(plugins)); - Ok(mu) + let plugins = Arc::new(plugins); + *INSTALL_STATE_PLUGINS.lock().unwrap() = Some(plugins.clone()); + Ok(plugins) } fn init_tools() -> MutexResult { - let mut mu = INSTALL_STATE_TOOLS.lock().unwrap(); - if mu.is_some() { - return Ok(mu); + if let Some(tools) = INSTALL_STATE_TOOLS.lock().unwrap().clone() { + return Ok(tools); } let mut tools = file::dir_subdirs(&dirs::INSTALLS)? .into_par_iter() @@ -105,7 +104,7 @@ fn init_tools() -> MutexResult { .flatten() .filter(|(_, tool)| !tool.versions.is_empty()) .collect::>(); - for (short, pt) in init_plugins()?.as_ref().unwrap().iter() { + for (short, pt) in init_plugins()?.iter() { let full = match pt { PluginType::Asdf => format!("asdf:{short}"), PluginType::Vfox => format!("vfox:{short}"), @@ -119,39 +118,34 @@ fn init_tools() -> MutexResult { }); tool.full = Some(full); } - *mu = Some(Arc::new(tools)); - Ok(mu) + let tools = Arc::new(tools); + *INSTALL_STATE_TOOLS.lock().unwrap() = Some(tools.clone()); + Ok(tools) } pub fn list_plugins() -> Result>> { let plugins = init_plugins()?; - Ok(plugins.as_ref().unwrap().clone()) + Ok(plugins) } pub fn get_tool_full(short: &str) -> Result> { let tools = init_tools()?; - Ok(tools - .as_ref() - .unwrap() - .get(short) - .and_then(|t| t.full.clone())) + Ok(tools.get(short).and_then(|t| t.full.clone())) } pub fn get_plugin_type(short: &str) -> Result> { let plugins = init_plugins()?; - Ok(plugins.as_ref().unwrap().get(short).cloned()) + Ok(plugins.get(short).cloned()) } pub fn list_tools() -> Result>> { let tools = init_tools()?; - Ok(tools.as_ref().unwrap().clone()) + Ok(tools) } pub fn backend_type(short: &str) -> Result> { let tools = init_tools()?; let backend_type = tools - .as_ref() - .unwrap() .get(short) .and_then(|ist| ist.full.as_ref()) .map(|full| BackendType::guess(full)); @@ -161,18 +155,15 @@ pub fn backend_type(short: &str) -> Result> { pub fn list_versions(short: &str) -> Result> { let tools = init_tools()?; Ok(tools - .as_ref() - .unwrap() .get(short) .map(|tool| tool.versions.clone()) .unwrap_or_default()) } pub fn add_plugin(short: &str, plugin_type: PluginType) -> Result<()> { - let mut p = init_plugins()?; - let mut plugins = p.take().unwrap().deref().clone(); + let mut plugins = init_plugins()?.deref().clone(); plugins.insert(short.to_string(), plugin_type); - *p = Some(Arc::new(plugins)); + *INSTALL_STATE_PLUGINS.lock().unwrap() = Some(Arc::new(plugins)); Ok(()) }