Skip to content
This repository was archived by the owner on Aug 20, 2021. It is now read-only.

Commit 50b7aa7

Browse files
committed
allow prefix to be any AsRef<OsStr> type, matching up with the contents of Path
1 parent 686165c commit 50b7aa7

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

src/lib.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use std::env;
1919
use std::io::{self, Error, ErrorKind};
2020
use std::fs;
2121
use std::path::{self, PathBuf, Path};
22+
use std::ffi::{OsStr, OsString};
2223
use rand::{thread_rng, Rng};
2324

2425
/// A wrapper for a path to temporary directory implementing automatic
@@ -42,7 +43,7 @@ impl TempDir {
4243
/// deleted once the returned wrapper is destroyed.
4344
///
4445
/// If no directory can be created, `Err` is returned.
45-
pub fn new_in<P: AsRef<Path>>(tmpdir: P, prefix: &str)
46+
pub fn new_in<P: AsRef<Path>, N: AsRef<OsStr>>(tmpdir: P, prefix: N)
4647
-> io::Result<TempDir> {
4748
let storage;
4849
let mut tmpdir = tmpdir.as_ref();
@@ -56,13 +57,18 @@ impl TempDir {
5657
let mut rng = thread_rng();
5758
for _ in 0..NUM_RETRIES {
5859
let suffix: String = rng.gen_ascii_chars().take(NUM_RAND_CHARS).collect();
59-
let leaf = if prefix.len() > 0 {
60-
format!("{}.{}", prefix, suffix)
60+
let pv = prefix.as_ref();
61+
let leaf = if pv != OsStr::new("") {
62+
let mut s = OsString::new();
63+
s.push(pv);
64+
s.push(".");
65+
s.push(suffix);
66+
s
6167
} else {
6268
// If we're given an empty string for a prefix, then creating a
6369
// directory starting with "." would lead to it being
6470
// semi-invisible on some systems.
65-
suffix
71+
OsString::from(suffix)
6672
};
6773
let path = tmpdir.join(&leaf);
6874
match fs::create_dir(&path) {

0 commit comments

Comments
 (0)