pub struct ResourceTracker { /* private fields */ }Expand description
Tracks resources created during compression/decompression for guaranteed cleanup.
Uses RAII pattern - resources are automatically cleaned up when dropped unless marked as complete. This ensures incomplete output files are deleted if an operation is cancelled or fails.
§Thread Safety
All methods use interior mutability and are safe to call from multiple threads.
§Example
use crush_core::cancel::ResourceTracker;
use std::path::PathBuf;
let tracker = ResourceTracker::new();
// Register the output file to be cleaned up if operation fails
tracker.register_output(PathBuf::from("output.crush"));
// Register temporary files that should always be deleted
tracker.register_temp_file(PathBuf::from("temp.dat"));
// ... do compression work ...
// If successful, mark complete to keep the output file
tracker.mark_complete();
// Drop will clean up temp files but keep output (marked complete)§Cleanup Behavior
- Temp files: Always deleted on drop
- Output file: Deleted on drop UNLESS
mark_complete()was called - On panic: Drop runs, ensuring cleanup even during unwinding
Implementations§
Source§impl ResourceTracker
impl ResourceTracker
Sourcepub fn register_output(&self, path: PathBuf)
pub fn register_output(&self, path: PathBuf)
Register the output file path to be cleaned up if the operation doesn’t complete.
The output file will be deleted on drop unless mark_complete() is called.
Only one output file can be registered (subsequent calls replace the previous).
§Arguments
path- Path to the output file
Sourcepub fn register_temp_file(&self, path: PathBuf)
pub fn register_temp_file(&self, path: PathBuf)
Register a temporary file that should always be deleted on cleanup.
Temporary files are always deleted on drop, regardless of completion status. Multiple temporary files can be registered.
§Arguments
path- Path to the temporary file
Sourcepub fn mark_complete(&self)
pub fn mark_complete(&self)
Mark the operation as successfully completed, preventing output file deletion.
Call this after the operation succeeds to keep the output file. If not called, the output file will be deleted on drop (cleanup on failure).
§Example
let tracker = ResourceTracker::new();
tracker.register_output(PathBuf::from("output.dat"));
// ... do work ...
if work_succeeded() {
tracker.mark_complete(); // Keep the output file
}
// If work failed, drop will delete the output fileSourcepub fn cleanup_all(&self) -> Result<()>
pub fn cleanup_all(&self) -> Result<()>
Clean up all tracked resources.
§Errors
Returns an error if file deletion fails (permissions, file in use, etc.). Returns the first error encountered if multiple deletions fail.