Skip to content

Commit a556900

Browse files
committed
Make std::io::TempDir::{new, new_in} accept a BytesContainer as a suffix.
1 parent f7d18b9 commit a556900

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

src/libstd/io/tempfile.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use ops::Drop;
1717
use option::Option;
1818
use option::Option::{None, Some};
1919
use os;
20-
use path::{Path, GenericPath};
20+
use path::{Path, GenericPath, BytesContainer};
2121
use result::Result::{Ok, Err};
2222
use sync::atomic;
2323

@@ -34,7 +34,7 @@ impl TempDir {
3434
/// deleted once the returned wrapper is destroyed.
3535
///
3636
/// If no directory can be created, `Err` is returned.
37-
pub fn new_in(tmpdir: &Path, suffix: &str) -> IoResult<TempDir> {
37+
pub fn new_in<T: BytesContainer>(tmpdir: &Path, suffix: T) -> IoResult<TempDir> {
3838
if !tmpdir.is_absolute() {
3939
let abs_tmpdir = try!(os::make_absolute(tmpdir));
4040
return TempDir::new_in(&abs_tmpdir, suffix);
@@ -44,11 +44,13 @@ impl TempDir {
4444

4545
let mut attempts = 0u;
4646
loop {
47-
let filename =
48-
format!("rs-{}-{}-{}",
47+
let prefix =
48+
format!("rs-{}-{}-",
4949
unsafe { libc::getpid() },
50-
CNT.fetch_add(1, atomic::SeqCst),
51-
suffix);
50+
CNT.fetch_add(1, atomic::SeqCst));
51+
52+
let mut filename = prefix.into_bytes();
53+
filename.push_all(suffix.container_as_bytes());
5254
let p = tmpdir.join(filename);
5355
match fs::mkdir(&p, io::USER_RWX) {
5456
Err(error) => {
@@ -67,7 +69,7 @@ impl TempDir {
6769
/// deleted once the returned wrapper is destroyed.
6870
///
6971
/// If no directory can be created, `Err` is returned.
70-
pub fn new(suffix: &str) -> IoResult<TempDir> {
72+
pub fn new<T: BytesContainer>(suffix: T) -> IoResult<TempDir> {
7173
TempDir::new_in(&os::tmpdir(), suffix)
7274
}
7375

src/test/run-pass/tempfile.rs

+8
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ fn test_tempdir() {
3232
p.clone()
3333
};
3434
assert!(!path.exists());
35+
36+
let path2 = {
37+
let p = TempDir::new_in(&Path::new("."), "foobar".as_bytes()).unwrap();
38+
let p = p.path();
39+
assert!(p.as_vec().ends_with(b"foobar"));
40+
p.clone()
41+
};
42+
assert!(!path2.exists());
3543
}
3644

3745
fn test_rm_tempdir() {

0 commit comments

Comments
 (0)