Skip to content

Commit e9acdd9

Browse files
committed
std: generlize & move io::file::suppressed_stat to io::ignore_io_error
1 parent 52840a5 commit e9acdd9

File tree

2 files changed

+19
-19
lines changed

2 files changed

+19
-19
lines changed

src/libstd/rt/io/file.rs

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use rt::rtio::{RtioFileStream, IoFactory, IoFactoryObject};
1717
use rt::io::{io_error, read_error, EndOfFile,
1818
FileMode, FileAccess, FileStat, IoError,
1919
PathAlreadyExists, PathDoesntExist,
20-
MismatchedFileTypeForOperation};
20+
MismatchedFileTypeForOperation, ignore_io_error};
2121
use rt::local::Local;
2222
use option::{Some, None};
2323
use path::Path;
@@ -248,18 +248,6 @@ impl Seek for FileStream {
248248
}
249249
}
250250

251-
// helper for grabbing a stat and ignoring any
252-
// error.. used in Info wrappers
253-
fn suppressed_stat(cb: &fn() -> Option<FileStat>) -> Option<FileStat> {
254-
do io_error::cond.trap(|_| {
255-
// just swallow the error.. downstream users
256-
// who can make a decision based on a None result
257-
// won't care
258-
}).inside {
259-
cb()
260-
}
261-
}
262-
263251
/// Shared functionality between `FileInfo` and `DirectoryInfo`
264252
pub trait FileSystemInfo {
265253
/// Get the filesystem path that this instance points at,
@@ -277,7 +265,7 @@ pub trait FileSystemInfo {
277265
/// returns `true` if the location pointed at by the enclosing
278266
/// exists on the filesystem
279267
fn exists(&self) -> bool {
280-
match suppressed_stat(|| self.stat()) {
268+
match ignore_io_error(|| self.stat()) {
281269
Some(_) => true,
282270
None => false
283271
}
@@ -306,7 +294,7 @@ pub trait FileInfo : FileSystemInfo {
306294
/// false for paths to non-existent locations or directories or
307295
/// other non-regular files (named pipes, etc).
308296
fn is_file(&self) -> bool {
309-
match suppressed_stat(|| self.stat()) {
297+
match ignore_io_error(|| self.stat()) {
310298
Some(s) => s.is_file,
311299
None => false
312300
}
@@ -315,7 +303,7 @@ pub trait FileInfo : FileSystemInfo {
315303
/// Attempts to open a regular file for reading/writing based
316304
/// on provided inputs
317305
fn open_stream(&self, mode: FileMode, access: FileAccess) -> Option<FileStream> {
318-
match suppressed_stat(|| self.stat()) {
306+
match ignore_io_error(|| self.stat()) {
319307
Some(s) => match s.is_file {
320308
true => open(self.get_path(), mode, access),
321309
false => None
@@ -364,7 +352,7 @@ trait DirectoryInfo : FileSystemInfo {
364352
/// false for paths to non-existent locations or if the item is
365353
/// not a directory (eg files, named pipes, links, etc)
366354
fn is_dir(&self) -> bool {
367-
match suppressed_stat(|| self.stat()) {
355+
match ignore_io_error(|| self.stat()) {
368356
Some(s) => s.is_dir,
369357
None => false
370358
}
@@ -375,7 +363,7 @@ trait DirectoryInfo : FileSystemInfo {
375363
/// at that location or if some other error occurs during
376364
/// the mkdir operation
377365
fn mkdir(&self) {
378-
match suppressed_stat(|| self.stat()) {
366+
match ignore_io_error(|| self.stat()) {
379367
Some(_) => {
380368
io_error::cond.raise(IoError {
381369
kind: PathAlreadyExists,
@@ -391,7 +379,7 @@ trait DirectoryInfo : FileSystemInfo {
391379
/// the type underlying the given `DirectoryInfo`. Will fail
392380
/// if there is no directory at the given location or if
393381
fn rmdir(&self) {
394-
match suppressed_stat(|| self.stat()) {
382+
match ignore_io_error(|| self.stat()) {
395383
Some(s) => {
396384
match s.is_dir {
397385
true => rmdir(self.get_path()),

src/libstd/rt/io/mod.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,18 @@ condition! {
401401
pub read_error: super::IoError -> ();
402402
}
403403

404+
/// Helper for wrapper calls where you want to
405+
/// ignore any io_errors that might be raised
406+
pub fn ignore_io_error<T>(cb: &fn() -> T) -> T {
407+
do io_error::cond.trap(|_| {
408+
// just swallow the error.. downstream users
409+
// who can make a decision based on a None result
410+
// won't care
411+
}).inside {
412+
cb()
413+
}
414+
}
415+
404416
pub trait Reader {
405417
/// Read bytes, up to the length of `buf` and place them in `buf`.
406418
/// Returns the number of bytes read. The number of bytes read my

0 commit comments

Comments
 (0)