Skip to main content

ckb_rocksdb/
util.rs

1// Copyright 2019 Tyler Neely
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14//
15
16use std::path::{Path, PathBuf};
17use tempfile::{TempDir, tempdir};
18
19/// Ensures that DB::Destroy is called and the directory is deleted
20/// for this database when TemporaryDBPath is dropped.
21pub struct TemporaryDBPath {
22    dir: TempDir,
23}
24
25impl TemporaryDBPath {
26    pub fn new() -> TemporaryDBPath {
27        let dir = tempdir().unwrap();
28        TemporaryDBPath { dir }
29    }
30
31    pub fn join<P: AsRef<Path>>(&self, path: P) -> PathBuf {
32        self.dir.path().join(path)
33    }
34}
35
36impl Default for TemporaryDBPath {
37    fn default() -> TemporaryDBPath {
38        let dir = tempdir().unwrap();
39        TemporaryDBPath { dir }
40    }
41}
42
43impl AsRef<Path> for TemporaryDBPath {
44    fn as_ref(&self) -> &Path {
45        self.dir.path()
46    }
47}