Parcourir la source

refactor: Migrate ALL_MOUNTS from lazy_static to OnceLock

Robert Minsk il y a 2 ans
Parent
commit
ae30003d6d
2 fichiers modifiés avec 9 ajouts et 8 suppressions
  1. 3 3
      src/fs/file.rs
  2. 6 5
      src/fs/mounts/mod.rs

+ 3 - 3
src/fs/file.rs

@@ -16,7 +16,7 @@ use crate::fs::feature::xattr;
 use crate::fs::feature::xattr::{FileAttributes, Attribute};
 use crate::fs::fields as f;
 
-use super::mounts::ALL_MOUNTS;
+use super::mounts::all_mounts;
 use super::mounts::MountedFs;
 
 
@@ -256,13 +256,13 @@ impl<'dir> File<'dir> {
     pub fn is_mount_point(&self) -> bool {
         cfg!(any(target_os = "linux", target_os = "macos")) &&
             self.is_directory() &&
-            self.absolute_path.as_ref().is_some_and(|p| ALL_MOUNTS.contains_key(p))
+            self.absolute_path.as_ref().is_some_and(|p| all_mounts().contains_key(p))
     }
 
     /// The filesystem device and type for a mount point
     pub fn mount_point_info(&self) -> Option<&MountedFs> {
         if cfg!(any(target_os = "linux",target_os = "macos")) {
-            return self.absolute_path.as_ref().and_then(|p|ALL_MOUNTS.get(p));
+            return self.absolute_path.as_ref().and_then(|p| all_mounts().get(p));
         }
         None
     }

+ 6 - 5
src/fs/mounts/mod.rs

@@ -1,7 +1,6 @@
-use lazy_static::lazy_static;
-
 use std::collections::HashMap;
 use std::path::PathBuf;
+use std::sync::OnceLock;
 
 #[cfg(target_os = "linux")]
 mod linux;
@@ -56,8 +55,10 @@ impl std::fmt::Display for Error {
 // up for every directory. Ideally this would only be done if the --mounts
 // option is specified which will be significantly easier once the move
 // to `clap` is complete.
-lazy_static! {
-    pub(crate) static ref ALL_MOUNTS: HashMap<PathBuf, MountedFs> = {
+pub(super) fn all_mounts() -> &'static HashMap<PathBuf, MountedFs> {
+    static ALL_MOUNTS: OnceLock<HashMap<PathBuf, MountedFs>> = OnceLock::new();
+
+    ALL_MOUNTS.get_or_init(|| {
         // Allow unused_mut for windows build
         #[allow(unused_mut)]
         let mut mount_map: HashMap<PathBuf, MountedFs> = HashMap::new();
@@ -70,5 +71,5 @@ lazy_static! {
         }
 
         mount_map
-    };
+    })
 }