kxio/src/fs/temp.rs

32 lines
697 B
Rust
Raw Normal View History

use std::sync::{Arc, Mutex};
use tempfile::TempDir;
2024-11-02 20:52:30 +00:00
use super::{Error, FileSystem};
2024-10-30 08:52:42 +00:00
#[derive(Clone, Debug)]
pub struct TempFileSystem {
2024-10-30 08:52:42 +00:00
real: FileSystem,
_temp_dir: Arc<Mutex<TempDir>>,
}
2024-11-01 18:52:11 +00:00
impl TempFileSystem {
pub fn new() -> super::Result<Self> {
2024-11-02 20:52:30 +00:00
let temp_dir = tempfile::tempdir().map_err(Error::Io)?;
2024-11-01 18:52:11 +00:00
let base = temp_dir.path().to_path_buf();
let temp_dir = Arc::new(Mutex::new(temp_dir));
let real = super::new(base);
2024-11-01 18:52:11 +00:00
Ok(Self {
real,
_temp_dir: temp_dir,
})
}
}
impl std::ops::Deref for TempFileSystem {
2024-10-30 08:52:42 +00:00
type Target = FileSystem;
fn deref(&self) -> &Self::Target {
&self.real
}
}