// Copyright © 2019-2020 The Radicle Foundation // // This file is part of radicle-link, distributed under the GPLv3 with Radicle // Linking Exception. For full terms see the included LICENSE file. use std::{ io, ops::{Deref, DerefMut}, path::Path, }; use tempfile::{tempdir, TempDir}; pub struct WithTmpDir { _tmp: TempDir, inner: A, } impl WithTmpDir { pub fn new(mk_inner: F) -> Result where F: FnOnce(&Path) -> Result, E: From, { let tmp = tempdir()?; let inner = mk_inner(tmp.path())?; Ok(Self { _tmp: tmp, inner }) } } impl Deref for WithTmpDir { type Target = A; fn deref(&self) -> &Self::Target { &self.inner } } impl DerefMut for WithTmpDir { fn deref_mut(&mut self) -> &mut Self::Target { &mut self.inner } }