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

Commit d989460

Browse files
committed
allow prefix to be any AsOsStr type, matching up with the contents of Path
1 parent 4065b42 commit d989460

1 file changed

Lines changed: 9 additions & 4 deletions

File tree

src/lib.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ extern crate rand;
1818

1919
use std::env;
2020
use std::io::{self, Error, ErrorKind};
21+
use std::ffi::{AsOsStr, OsStr, OsString};
2122
use std::fs;
2223
use std::path::{self, PathBuf, AsPath};
2324
use rand::{thread_rng, Rng};
@@ -43,7 +44,7 @@ impl TempDir {
4344
/// deleted once the returned wrapper is destroyed.
4445
///
4546
/// If no directory can be created, `Err` is returned.
46-
pub fn new_in<P: AsPath + ?Sized>(tmpdir: &P, prefix: &str)
47+
pub fn new_in<P: AsPath + ?Sized, N: AsOsStr + ?Sized>(tmpdir: &P, prefix: &N)
4748
-> io::Result<TempDir> {
4849
let storage;
4950
let mut tmpdir = tmpdir.as_path();
@@ -57,13 +58,17 @@ impl TempDir {
5758
let mut rng = thread_rng();
5859
for _ in 0..NUM_RETRIES {
5960
let suffix: String = rng.gen_ascii_chars().take(NUM_RAND_CHARS).collect();
60-
let leaf = if prefix.len() > 0 {
61-
format!("{}.{}", prefix, suffix)
61+
let leaf = if prefix.as_os_str() != OsStr::from_str("") {
62+
let mut s = OsString::new();
63+
s.push(prefix.as_os_str());
64+
s.push(OsStr::from_str("."));
65+
s.push(suffix.as_os_str());
66+
s
6267
} else {
6368
// If we're given an empty string for a prefix, then creating a
6469
// directory starting with "." would lead to it being
6570
// semi-invisible on some systems.
66-
suffix
71+
suffix.as_os_str().to_os_string()
6772
};
6873
let path = tmpdir.join(&leaf);
6974
match fs::create_dir(&path) {

0 commit comments

Comments
 (0)