From a556900cd6c8c7750c8c5c5dca86a1c573a6cf15 Mon Sep 17 00:00:00 2001
From: Chase Southwood <chase.southwood@gmail.com>
Date: Mon, 1 Dec 2014 18:25:05 -0600
Subject: [PATCH] Make std::io::TempDir::{new, new_in} accept a BytesContainer
 as a suffix.

---
 src/libstd/io/tempfile.rs     | 16 +++++++++-------
 src/test/run-pass/tempfile.rs |  8 ++++++++
 2 files changed, 17 insertions(+), 7 deletions(-)

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<TempDir> {
+    pub fn new_in<T: BytesContainer>(tmpdir: &Path, suffix: T) -> IoResult<TempDir> {
         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<TempDir> {
+    pub fn new<T: BytesContainer>(suffix: T) -> IoResult<TempDir> {
         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() {