Skip to content

Commit 2cd850b

Browse files
committed
tempdir: Add a persist method
I have a use case where I want the equivalent of `into_path()`. Signed-off-by: Colin Walters <[email protected]>
1 parent 076ab49 commit 2cd850b

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

Diff for: cap-tempfile/src/lib.rs

+18
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,15 @@ impl TempDir {
101101
Err(Self::already_exists())
102102
}
103103

104+
/// Make this directory persistent.
105+
///
106+
/// This corresponds to [`tempfile::TempDir::into_path`].
107+
///
108+
/// [`tempfile::TempDir::into_path`]: https://docs.rs/tempfile/latest/tempfile/struct.TempDir.html#method.into_path
109+
pub fn persist(mut self) -> io::Result<Dir> {
110+
Ok(self.dir.take().unwrap())
111+
}
112+
104113
/// Closes and removes the temporary directory, returning a `Result`.
105114
///
106115
/// This corresponds to [`tempfile::TempDir::close`].
@@ -222,6 +231,15 @@ fn close_tempdir() {
222231
t.close().unwrap();
223232
}
224233

234+
#[test]
235+
fn persist_tempdir() {
236+
use crate::ambient_authority;
237+
238+
let t = tempdir(ambient_authority()).unwrap();
239+
let d = t.persist().unwrap();
240+
assert!(d.exists("."));
241+
}
242+
225243
#[test]
226244
fn drop_tempdir_in() {
227245
use crate::ambient_authority;

0 commit comments

Comments
 (0)