Skip to main content

ResourceTracker

Struct ResourceTracker 

Source
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

Source

pub fn new() -> Self

Create a new resource tracker.

Source

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
Source

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
Source

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 file
Source

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.

Trait Implementations§

Source§

impl Default for ResourceTracker

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Drop for ResourceTracker

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.