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

allow prefix to be any AsOsStr type, matching up with the contents of Path #1

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use std::env;
use std::io::{self, Error, ErrorKind};
use std::fs;
use std::path::{self, PathBuf, Path};
use std::ffi::{OsStr, OsString};
use rand::{thread_rng, Rng};

/// A wrapper for a path to temporary directory implementing automatic
Expand All @@ -42,7 +43,7 @@ impl TempDir {
/// deleted once the returned wrapper is destroyed.
///
/// If no directory can be created, `Err` is returned.
pub fn new_in<P: AsRef<Path>>(tmpdir: P, prefix: &str)
pub fn new_in<P: AsRef<Path>, N: AsRef<OsStr>>(tmpdir: P, prefix: N)
-> io::Result<TempDir> {
let storage;
let mut tmpdir = tmpdir.as_ref();
Expand All @@ -56,13 +57,18 @@ impl TempDir {
let mut rng = thread_rng();
for _ in 0..NUM_RETRIES {
let suffix: String = rng.gen_ascii_chars().take(NUM_RAND_CHARS).collect();
let leaf = if prefix.len() > 0 {
format!("{}.{}", prefix, suffix)
let pv = prefix.as_ref();
let leaf = if pv != OsStr::new("") {
let mut s = OsString::new();
s.push(pv);
s.push(".");
s.push(suffix);
s
} else {
// If we're given an empty string for a prefix, then creating a
// directory starting with "." would lead to it being
// semi-invisible on some systems.
suffix
OsString::from(suffix)
};
let path = tmpdir.join(&leaf);
match fs::create_dir(&path) {
Expand Down