pub struct TempDir { /* private fields */ }Expand description
A temporary directory that automatically cleans up when dropped.
This struct provides RAII (Resource Acquisition Is Initialization) semantics for temporary directories. The directory is created when the struct is created and automatically removed when the struct is dropped, even if the program panics.
§Examples
use agpm_cli::utils::fs::TempDir;
{
let temp = TempDir::new("test")?;
let temp_path = temp.path();
// Use the temporary directory
std::fs::write(temp_path.join("file.txt"), "temporary data")?;
// Directory exists here
assert!(temp_path.exists());
} // TempDir is dropped here, directory is automatically cleaned up§Thread Safety
Each TempDir instance creates a unique directory using UUID generation,
making it safe to use across multiple threads without naming conflicts.
§Cleanup Behavior
- Directory is removed recursively when dropped
- Cleanup happens even if the program panics
- If cleanup fails (rare), the error is silently ignored
- Uses the system temporary directory as the parent
§Use Cases
- Unit testing with temporary files
- Staging areas for atomic operations
- Scratch space for temporary processing
Implementations§
Source§impl TempDir
impl TempDir
Sourcepub fn new(prefix: &str) -> Result<Self>
pub fn new(prefix: &str) -> Result<Self>
Creates a new temporary directory with the given prefix.
The directory is created immediately and will have a name like
agpm_{prefix}_{uuid} in the system temporary directory.
§Arguments
prefix- A prefix for the directory name (for identification)
§Returns
A new TempDir instance, or an error if directory creation fails
§Examples
use agpm_cli::utils::fs::TempDir;
let temp = TempDir::new("cache")?;
println!("Temporary directory: {}", temp.path().display());Trait Implementations§
Auto Trait Implementations§
impl Freeze for TempDir
impl RefUnwindSafe for TempDir
impl Send for TempDir
impl Sync for TempDir
impl Unpin for TempDir
impl UnwindSafe for TempDir
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more