diff --git a/src/libstd/io/tempfile.rs b/src/libstd/io/tempfile.rs index f3a119399952d..b75c4ef7d40b5 100644 --- a/src/libstd/io/tempfile.rs +++ b/src/libstd/io/tempfile.rs @@ -17,7 +17,7 @@ use ops::Drop; use option::Option; use option::Option::{None, Some}; use os; -use path::{Path, GenericPath}; +use path::{Path, GenericPath, BytesContainer}; use result::Result::{Ok, Err}; use sync::atomic; @@ -34,7 +34,7 @@ impl TempDir { /// deleted once the returned wrapper is destroyed. /// /// If no directory can be created, `Err` is returned. - pub fn new_in(tmpdir: &Path, suffix: &str) -> IoResult { + pub fn new_in(tmpdir: &Path, suffix: T) -> IoResult { if !tmpdir.is_absolute() { let abs_tmpdir = try!(os::make_absolute(tmpdir)); return TempDir::new_in(&abs_tmpdir, suffix); @@ -44,11 +44,13 @@ impl TempDir { let mut attempts = 0u; loop { - let filename = - format!("rs-{}-{}-{}", + let prefix = + format!("rs-{}-{}-", unsafe { libc::getpid() }, - CNT.fetch_add(1, atomic::SeqCst), - suffix); + CNT.fetch_add(1, atomic::SeqCst)); + + let mut filename = prefix.into_bytes(); + filename.push_all(suffix.container_as_bytes()); let p = tmpdir.join(filename); match fs::mkdir(&p, io::USER_RWX) { Err(error) => { @@ -67,7 +69,7 @@ impl TempDir { /// deleted once the returned wrapper is destroyed. /// /// If no directory can be created, `Err` is returned. - pub fn new(suffix: &str) -> IoResult { + pub fn new(suffix: T) -> IoResult { TempDir::new_in(&os::tmpdir(), suffix) } diff --git a/src/test/run-pass/tempfile.rs b/src/test/run-pass/tempfile.rs index 7400c52a73b4a..08b15d8f170c4 100644 --- a/src/test/run-pass/tempfile.rs +++ b/src/test/run-pass/tempfile.rs @@ -32,6 +32,14 @@ fn test_tempdir() { p.clone() }; assert!(!path.exists()); + + let path2 = { + let p = TempDir::new_in(&Path::new("."), "foobar".as_bytes()).unwrap(); + let p = p.path(); + assert!(p.as_vec().ends_with(b"foobar")); + p.clone() + }; + assert!(!path2.exists()); } fn test_rm_tempdir() {