Skip to content

Commit f85d3b3

Browse files
committed
std: reform fn sigs of FileDescriptor methods (better result signalling)
1 parent 429b5f8 commit f85d3b3

File tree

1 file changed

+46
-42
lines changed

1 file changed

+46
-42
lines changed

src/libstd/rt/uv/file.rs

Lines changed: 46 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use prelude::*;
1212
use ptr::null;
1313
use libc::c_void;
1414
use rt::uv::{Request, NativeHandle, Loop, FsCallback, Buf,
15-
status_to_maybe_uv_error_with_loop};
15+
status_to_maybe_uv_error_with_loop, UvError};
1616
use rt::uv::uvll;
1717
use rt::uv::uvll::*;
1818
use super::super::io::support::PathLike;
@@ -86,13 +86,21 @@ impl NativeHandle<*uvll::uv_fs_t> for FsRequest {
8686
match self { &FsRequest(ptr) => ptr }
8787
}
8888
}
89+
fn sync_cleanup(loop_: &Loop, result: int)
90+
-> Result<int, UvError> {
91+
match status_to_maybe_uv_error_with_loop(loop_.native_handle(), result as i32) {
92+
Some(err) => Err(err),
93+
None => Ok(result)
94+
}
95+
}
8996

9097
pub struct FileDescriptor(c_int);
9198
impl FileDescriptor {
9299
fn new(fd: c_int) -> FileDescriptor {
93100
FileDescriptor(fd)
94101
}
95102

103+
96104
pub fn from_open_req(req: &mut FsRequest) -> FileDescriptor {
97105
FileDescriptor::new(req.get_result())
98106
}
@@ -115,12 +123,13 @@ impl FileDescriptor {
115123
result
116124
}
117125
pub fn open<P: PathLike>(loop_: Loop, path: &P, flags: int, mode: int,
118-
cb: FsCallback) -> int {
119-
FileDescriptor::open_common(loop_, path, flags, mode, Some(cb))
126+
cb: FsCallback) {
127+
FileDescriptor::open_common(loop_, path, flags, mode, Some(cb));
120128
}
121129

122-
pub fn open_sync<P: PathLike>(loop_: Loop, path: &P, flags: int, mode: int) -> int {
123-
FileDescriptor::open_common(loop_, path, flags, mode, None)
130+
pub fn open_sync<P: PathLike>(loop_: Loop, path: &P, flags: int, mode: int) -> Result<int, UvError> {
131+
let result = FileDescriptor::open_common(loop_, path, flags, mode, None);
132+
sync_cleanup(&loop_, result)
124133
}
125134

126135
fn unlink_common<P: PathLike>(loop_: Loop, path: &P, cb: Option<FsCallback>) -> int {
@@ -139,11 +148,13 @@ impl FileDescriptor {
139148
if is_sync { req.cleanup_and_delete(); }
140149
result
141150
}
142-
pub fn unlink<P: PathLike>(loop_: Loop, path: &P, cb: FsCallback) -> int {
143-
FileDescriptor::unlink_common(loop_, path, Some(cb))
151+
pub fn unlink<P: PathLike>(loop_: Loop, path: &P, cb: FsCallback) {
152+
let result = FileDescriptor::unlink_common(loop_, path, Some(cb));
153+
sync_cleanup(&loop_, result);
144154
}
145-
pub fn unlink_sync<P: PathLike>(loop_: Loop, path: &P) -> int {
146-
FileDescriptor::unlink_common(loop_, path, None)
155+
pub fn unlink_sync<P: PathLike>(loop_: Loop, path: &P) -> Result<int, UvError> {
156+
let result = FileDescriptor::unlink_common(loop_, path, None);
157+
sync_cleanup(&loop_, result)
147158
}
148159

149160
// as per bnoordhuis in #libuv: offset >= 0 uses prwrite instead of write
@@ -166,13 +177,13 @@ impl FileDescriptor {
166177
if is_sync { req.cleanup_and_delete(); }
167178
result
168179
}
169-
pub fn write(&mut self, loop_: Loop, buf: Buf, offset: i64, cb: FsCallback)
170-
-> int {
171-
self.write_common(loop_, buf, offset, Some(cb))
180+
pub fn write(&mut self, loop_: Loop, buf: Buf, offset: i64, cb: FsCallback) {
181+
self.write_common(loop_, buf, offset, Some(cb));
172182
}
173183
pub fn write_sync(&mut self, loop_: Loop, buf: Buf, offset: i64)
174-
-> int {
175-
self.write_common(loop_, buf, offset, None)
184+
-> Result<int, UvError> {
185+
let result = self.write_common(loop_, buf, offset, None);
186+
sync_cleanup(&loop_, result)
176187
}
177188

178189
fn read_common(&mut self, loop_: Loop, buf: Buf,
@@ -194,13 +205,13 @@ impl FileDescriptor {
194205
if is_sync { req.cleanup_and_delete(); }
195206
result
196207
}
197-
pub fn read(&mut self, loop_: Loop, buf: Buf, offset: i64, cb: FsCallback)
198-
-> int {
199-
self.read_common(loop_, buf, offset, Some(cb))
208+
pub fn read(&mut self, loop_: Loop, buf: Buf, offset: i64, cb: FsCallback) {
209+
self.read_common(loop_, buf, offset, Some(cb));
200210
}
201211
pub fn read_sync(&mut self, loop_: Loop, buf: Buf, offset: i64)
202-
-> int {
203-
self.read_common(loop_, buf, offset, None)
212+
-> Result<int, UvError> {
213+
let result = self.read_common(loop_, buf, offset, None);
214+
sync_cleanup(&loop_, result)
204215
}
205216

206217
fn close_common(self, loop_: Loop, cb: Option<FsCallback>) -> int {
@@ -217,11 +228,12 @@ impl FileDescriptor {
217228
if is_sync { req.cleanup_and_delete(); }
218229
result
219230
}
220-
pub fn close(self, loop_: Loop, cb: FsCallback) -> int {
221-
self.close_common(loop_, Some(cb))
231+
pub fn close(self, loop_: Loop, cb: FsCallback) {
232+
self.close_common(loop_, Some(cb));
222233
}
223-
pub fn close_sync(self, loop_: Loop) -> int {
224-
self.close_common(loop_, None)
234+
pub fn close_sync(self, loop_: Loop) -> Result<int, UvError> {
235+
let result = self.close_common(loop_, None);
236+
sync_cleanup(&loop_, result)
225237
}
226238
}
227239
extern fn compl_cb(req: *uv_fs_t) {
@@ -265,8 +277,7 @@ mod test {
265277
use str;
266278
use unstable::run_in_bare_thread;
267279
use path::Path;
268-
use rt::uv::{Loop, Buf, slice_to_uv_buf,
269-
status_to_maybe_uv_error_with_loop};
280+
use rt::uv::{Loop, Buf, slice_to_uv_buf};
270281
use libc::{O_CREAT, O_RDWR, O_RDONLY,
271282
S_IWUSR, S_IRUSR}; //NOTE: need defs for S_**GRP|S_**OTH in libc:: ...
272283
//S_IRGRP, S_IROTH};
@@ -358,31 +369,26 @@ mod test {
358369
// open/create
359370
let result = FileDescriptor::open_sync(loop_, &Path(path_str),
360371
create_flags as int, mode as int);
361-
assert!(status_to_maybe_uv_error_with_loop(
362-
loop_.native_handle(), result as i32).is_none());
363-
let mut fd = FileDescriptor(result as i32);
372+
assert!(result.is_ok());
373+
let mut fd = FileDescriptor(result.unwrap() as i32);
364374
// write
365375
let result = fd.write_sync(loop_, write_buf, -1);
366-
assert!(status_to_maybe_uv_error_with_loop(
367-
loop_.native_handle(), result as i32).is_none());
376+
assert!(result.is_ok());
368377
// close
369378
let result = fd.close_sync(loop_);
370-
assert!(status_to_maybe_uv_error_with_loop(
371-
loop_.native_handle(), result as i32).is_none());
379+
assert!(result.is_ok());
372380
// re-open
373381
let result = FileDescriptor::open_sync(loop_, &Path(path_str),
374382
read_flags as int,0);
375-
assert!(status_to_maybe_uv_error_with_loop(
376-
loop_.native_handle(), result as i32).is_none());
383+
assert!(result.is_ok());
377384
let len = 1028;
378-
let mut fd = FileDescriptor(result as i32);
385+
let mut fd = FileDescriptor(result.unwrap() as i32);
379386
// read
380387
let read_mem: ~[u8] = vec::from_elem(len, 0u8);
381388
let buf = slice_to_uv_buf(read_mem);
382389
let result = fd.read_sync(loop_, buf, 0);
383-
assert!(status_to_maybe_uv_error_with_loop(
384-
loop_.native_handle(), result as i32).is_none());
385-
let nread = result;
390+
assert!(result.is_ok());
391+
let nread = result.unwrap();
386392
// nread == 0 would be EOF.. we know it's >= zero because otherwise
387393
// the above assert would fail
388394
if nread > 0 {
@@ -391,12 +397,10 @@ mod test {
391397
assert!(read_str == ~"hello");
392398
// close
393399
let result = fd.close_sync(loop_);
394-
assert!(status_to_maybe_uv_error_with_loop(
395-
loop_.native_handle(), result as i32).is_none());
400+
assert!(result.is_ok());
396401
// unlink
397402
let result = FileDescriptor::unlink_sync(loop_, &Path(path_str));
398-
assert!(status_to_maybe_uv_error_with_loop(
399-
loop_.native_handle(), result as i32).is_none());
403+
assert!(result.is_ok());
400404
} else { fail!("nread was 0.. wudn't expectin' that."); }
401405
loop_.close();
402406
}

0 commit comments

Comments
 (0)