diff options
| author | Tim Chevalier <chevalier@alum.wellesley.edu> | 2012-06-21 21:30:16 -0700 |
|---|---|---|
| committer | Tim Chevalier <chevalier@alum.wellesley.edu> | 2012-06-21 21:30:16 -0700 |
| commit | fee78d296c66e0b10600c7fefbbe07f33406fe97 (patch) | |
| tree | da76576b422f5bd82cc091576a9f15fdab95563d /src/libcore/io.rs | |
| parent | b8710de5fffdc45c19ccc27ad8ed98c1ee51c025 (diff) | |
Port resources to classes in libcore
Diffstat (limited to 'src/libcore/io.rs')
| -rw-r--r-- | src/libcore/io.rs | 34 |
1 files changed, 23 insertions, 11 deletions
diff --git a/src/libcore/io.rs b/src/libcore/io.rs index 635b3f9ebf4..df1cf16781d 100644 --- a/src/libcore/io.rs +++ b/src/libcore/io.rs @@ -224,7 +224,11 @@ impl <T: reader, C> of reader for {base: T, cleanup: C} { fn tell() -> uint { self.base.tell() } } -resource FILE_res(f: *libc::FILE) { libc::fclose(f); } +class FILE_res { + let f: *libc::FILE; + new(f: *libc::FILE) { self.f = f; } + drop { libc::fclose(self.f); } +} fn FILE_reader(f: *libc::FILE, cleanup: bool) -> reader { if cleanup { @@ -383,7 +387,11 @@ impl of writer for fd_t { fn flush() -> int { 0 } } -resource fd_res(fd: fd_t) { libc::close(fd); } +class fd_res { + let fd: fd_t; + new(fd: fd_t) { self.fd = fd; } + drop { libc::close(self.fd); } +} fn fd_writer(fd: fd_t, cleanup: bool) -> writer { if cleanup { @@ -695,13 +703,17 @@ mod fsync { } - // Resource of artifacts that need to fsync on destruction - resource res<t>(arg: arg<t>) { - alt arg.opt_level { - option::none { } - option::some(level) { - // fail hard if not succesful - assert(arg.fsync_fn(arg.val, level) != -1); + // Artifacts that need to fsync on destruction + class res<t> { + let arg: arg<t>; + new(-arg: arg<t>) { self.arg <- arg; } + drop { + alt self.arg.opt_level { + option::none { } + option::some(level) { + // fail hard if not succesful + assert(self.arg.fsync_fn(self.arg.val, level) != -1); + } } } } @@ -718,7 +730,7 @@ mod fsync { fn FILE_res_sync(&&file: FILE_res, opt_level: option<level>, blk: fn(&&res<*libc::FILE>)) { blk(res({ - val: *file, opt_level: opt_level, + val: file.f, opt_level: opt_level, fsync_fn: fn@(&&file: *libc::FILE, l: level) -> int { ret os::fsync_fd(libc::fileno(file), l) as int; } @@ -729,7 +741,7 @@ mod fsync { fn fd_res_sync(&&fd: fd_res, opt_level: option<level>, blk: fn(&&res<fd_t>)) { blk(res({ - val: *fd, opt_level: opt_level, + val: fd.fd, opt_level: opt_level, fsync_fn: fn@(&&fd: fd_t, l: level) -> int { ret os::fsync_fd(fd, l) as int; } |
