diff options
| author | bors <bors@rust-lang.org> | 2014-09-06 08:01:33 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2014-09-06 08:01:33 +0000 |
| commit | 20c0ba1279efb5d40fcd4a739a50d09e48e0b37f (patch) | |
| tree | d6ead4586f68946d11ae2daede995262a092f71b /src/libstd | |
| parent | 4bea7b3ed0856310fc64614e5bb01e348777c99f (diff) | |
| parent | a049fb98cd399cb0d15ed8dc3bc5b8d9b327116b (diff) | |
| download | rust-20c0ba1279efb5d40fcd4a739a50d09e48e0b37f.tar.gz rust-20c0ba1279efb5d40fcd4a739a50d09e48e0b37f.zip | |
auto merge of #16907 : SimonSapin/rust/tempdir-result, r=huonw
This allows using `try!()` [breaking-change] Fixes #16875
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/io/tempfile.rs | 22 |
1 files changed, 13 insertions, 9 deletions
diff --git a/src/libstd/io/tempfile.rs b/src/libstd/io/tempfile.rs index 8def5d5c997..6c9f10e19ae 100644 --- a/src/libstd/io/tempfile.rs +++ b/src/libstd/io/tempfile.rs @@ -12,7 +12,6 @@ use io::{fs, IoResult}; use io; -use iter::range; use libc; use ops::Drop; use option::{Option, None, Some}; @@ -33,15 +32,16 @@ impl TempDir { /// will have the suffix `suffix`. The directory will be automatically /// deleted once the returned wrapper is destroyed. /// - /// If no directory can be created, None is returned. - pub fn new_in(tmpdir: &Path, suffix: &str) -> Option<TempDir> { + /// If no directory can be created, `Err` is returned. + pub fn new_in(tmpdir: &Path, suffix: &str) -> IoResult<TempDir> { if !tmpdir.is_absolute() { return TempDir::new_in(&os::make_absolute(tmpdir), suffix); } static mut CNT: atomic::AtomicUint = atomic::INIT_ATOMIC_UINT; - for _ in range(0u, 1000) { + let mut attempts = 0u; + loop { let filename = format!("rs-{}-{}-{}", unsafe { libc::getpid() }, @@ -49,19 +49,23 @@ impl TempDir { suffix); let p = tmpdir.join(filename); match fs::mkdir(&p, io::UserRWX) { - Err(..) => {} - Ok(()) => return Some(TempDir { path: Some(p), disarmed: false }) + Err(error) => { + if attempts >= 1000 { + return Err(error) + } + attempts += 1; + } + Ok(()) => return Ok(TempDir { path: Some(p), disarmed: false }) } } - None } /// Attempts to make a temporary directory inside of `os::tmpdir()` whose /// name will have the suffix `suffix`. The directory will be automatically /// deleted once the returned wrapper is destroyed. /// - /// If no directory can be created, None is returned. - pub fn new(suffix: &str) -> Option<TempDir> { + /// If no directory can be created, `Err` is returned. + pub fn new(suffix: &str) -> IoResult<TempDir> { TempDir::new_in(&os::tmpdir(), suffix) } |
