diff options
| author | 魏曹先生 <1992414357@qq.com> | 2026-06-27 18:52:49 +0800 |
|---|---|---|
| committer | 魏曹先生 <1992414357@qq.com> | 2026-06-27 18:52:49 +0800 |
| commit | 353fdc5b539aae0479c7404d0ed6404f82bf633a (patch) | |
| tree | 0f0f07080aebf2ad8f0be537c7bc855618d89df6 /mingling/src/res/dirs/temp_dir.rs | |
| parent | 5a23e6b3ad655b15b412720ab81b0508866bebce (diff) | |
feat(mingling): add directory environment resources and setup
Add four new resource types for common directory paths and a
convenience setup struct that registers all of them at once.
Diffstat (limited to 'mingling/src/res/dirs/temp_dir.rs')
| -rw-r--r-- | mingling/src/res/dirs/temp_dir.rs | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/mingling/src/res/dirs/temp_dir.rs b/mingling/src/res/dirs/temp_dir.rs new file mode 100644 index 0000000..f4f0dca --- /dev/null +++ b/mingling/src/res/dirs/temp_dir.rs @@ -0,0 +1,77 @@ +use std::{ + env::temp_dir, + path::{Path, PathBuf}, +}; + +/// A global resource that provides the system temporary directory path. +/// +/// This struct wraps a `PathBuf` representing the platform's temporary directory +/// (e.g., `/tmp` on Unix, `%TEMP%` on Windows). +/// +/// Note that `std::env::temp_dir()` is infallible — it always returns a path, +/// falling back to a platform-specific default (e.g., `/tmp` on Unix) if the +/// environment variable is not set. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct ResTempDir { + tmp: PathBuf, +} + +impl ResTempDir { + /// Creates a new `ResTempDir` by querying the OS for the system temporary directory. + /// + /// This method is infallible since `std::env::temp_dir()` always succeeds, + /// returning a platform-specific default when environment variables are unset. + pub fn new() -> Self { + Self { tmp: temp_dir() } + } +} + +impl Default for ResTempDir { + fn default() -> Self { + Self::new() + } +} + +impl From<PathBuf> for ResTempDir { + fn from(path: PathBuf) -> Self { + Self { tmp: path } + } +} + +impl From<&Path> for ResTempDir { + fn from(path: &Path) -> Self { + Self { tmp: path.to_path_buf() } + } +} + +impl From<&PathBuf> for ResTempDir { + fn from(path: &PathBuf) -> Self { + Self { tmp: path.clone() } + } +} + +impl From<ResTempDir> for PathBuf { + fn from(res: ResTempDir) -> Self { + res.tmp + } +} + +impl AsRef<Path> for ResTempDir { + fn as_ref(&self) -> &Path { + self.tmp.as_path() + } +} + +impl std::ops::Deref for ResTempDir { + type Target = PathBuf; + + fn deref(&self) -> &Self::Target { + &self.tmp + } +} + +impl std::ops::DerefMut for ResTempDir { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.tmp + } +} |
