Skip to content

Make std::io::TempDir::{new, new_in} accept a BytesContainer as a suffix #19447

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
16 changes: 9 additions & 7 deletions src/libstd/io/tempfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
Expand All @@ -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) => {
Expand All @@ -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)
}

Expand Down
8 changes: 8 additions & 0 deletions src/test/run-pass/tempfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down