1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//! Self-cleaning temporary locations for tests and benchmarks: each lives in
//! its own fresh directory, removed with everything in it (a store's WAL and
//! layout siblings included) when the guard drops, also while a failed test
//! unwinds.
use std::path::{Path, PathBuf};
/// A temporary store file or directory; see the module docs.
#[derive(Debug)]
pub struct TempPath {
dir: PathBuf,
path: PathBuf,
}
impl TempPath {
/// A store file path (`<fresh dir>/store.redb`) named after `prefix`.
///
/// # Panics
/// Panics when the temporary directory cannot be created.
#[must_use]
pub fn store(prefix: &str) -> Self {
let dir = Self::fresh(prefix);
let path = dir.join("store.redb");
Self { dir, path }
}
/// A fresh, empty directory named after `prefix`.
///
/// # Panics
/// Panics when the temporary directory cannot be created.
#[must_use]
pub fn dir(prefix: &str) -> Self {
let dir = Self::fresh(prefix);
Self {
path: dir.clone(),
dir,
}
}
fn fresh(prefix: &str) -> PathBuf {
let dir = std::env::temp_dir().join(format!("rushdb-{prefix}-{}", uuid::Uuid::now_v7()));
std::fs::create_dir_all(&dir).expect("create a temporary directory");
dir
}
/// The path itself.
#[must_use]
pub fn path(&self) -> &Path {
&self.path
}
}
impl std::ops::Deref for TempPath {
type Target = Path;
fn deref(&self) -> &Path {
&self.path
}
}
impl AsRef<Path> for TempPath {
fn as_ref(&self) -> &Path {
&self.path
}
}
impl Drop for TempPath {
fn drop(&mut self) {
let _ = std::fs::remove_dir_all(&self.dir);
}
}