Skip to content

Commit d0c589d

Browse files
committed
Hide unnecessary error checking from the user
This affects the `set_non_blocking` function which cannot fail for Unix or Windows, given correct parameters. Additionally, the short UDP write error case has been removed as there is no such thing as "short UDP writes", instead, the operating system will error out if the application tries to send a packet larger than the MTU of the network path.
1 parent f0f7ca2 commit d0c589d

File tree

8 files changed

+35
-29
lines changed

8 files changed

+35
-29
lines changed

src/libstd/sys/common/net.rs

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ pub fn connect_timeout(fd: sock_t,
503503
#[cfg(windows)] use libc::WSAEWOULDBLOCK as WOULDBLOCK;
504504

505505
// Make sure the call to connect() doesn't block
506-
try!(set_nonblocking(fd, true));
506+
set_nonblocking(fd, true);
507507

508508
let ret = match unsafe { libc::connect(fd, addrp, len) } {
509509
// If the connection is in progress, then we need to wait for it to
@@ -533,7 +533,7 @@ pub fn connect_timeout(fd: sock_t,
533533
};
534534

535535
// be sure to turn blocking I/O back on
536-
try!(set_nonblocking(fd, false));
536+
set_nonblocking(fd, false);
537537
return ret;
538538

539539
#[cfg(unix)]
@@ -626,7 +626,7 @@ pub struct Guard<'a> {
626626
#[unsafe_destructor]
627627
impl<'a> Drop for Guard<'a> {
628628
fn drop(&mut self) {
629-
assert!(set_nonblocking(self.fd, false).is_ok());
629+
set_nonblocking(self.fd, false);
630630
}
631631
}
632632

@@ -723,7 +723,7 @@ impl TcpStream {
723723
fd: self.fd(),
724724
guard: self.inner.lock.lock().unwrap(),
725725
};
726-
assert!(set_nonblocking(self.fd(), true).is_ok());
726+
set_nonblocking(self.fd(), true);
727727
ret
728728
}
729729

@@ -862,7 +862,7 @@ impl UdpSocket {
862862
fd: self.fd(),
863863
guard: self.inner.lock.lock().unwrap(),
864864
};
865-
assert!(set_nonblocking(self.fd(), true).is_ok());
865+
set_nonblocking(self.fd(), true);
866866
ret
867867
}
868868

@@ -887,9 +887,7 @@ impl UdpSocket {
887887
storagep,
888888
&mut addrlen) as libc::c_int
889889
}));
890-
sockaddr_to_addr(&storage, addrlen as uint).and_then(|addr| {
891-
Ok((n as uint, addr))
892-
})
890+
Ok((n as uint, sockaddr_to_addr(&storage, addrlen as uint).unwrap()))
893891
}
894892

895893
pub fn send_to(&mut self, buf: &[u8], dst: SocketAddr) -> IoResult<()> {
@@ -910,11 +908,8 @@ impl UdpSocket {
910908
};
911909

912910
let n = try!(write(fd, self.write_deadline, buf, false, dolock, dowrite));
913-
if n != buf.len() {
914-
Err(short_write(n, "couldn't send entire packet at once"))
915-
} else {
916-
Ok(())
917-
}
911+
assert!(n == buf.len(), "UDP packet not completely written.");
912+
Ok(())
918913
}
919914

920915
pub fn join_multicast(&mut self, multi: IpAddr) -> IoResult<()> {

src/libstd/sys/unix/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,9 +214,9 @@ pub fn wouldblock() -> bool {
214214
err == libc::EWOULDBLOCK as i32 || err == libc::EAGAIN as i32
215215
}
216216

217-
pub fn set_nonblocking(fd: sock_t, nb: bool) -> IoResult<()> {
217+
pub fn set_nonblocking(fd: sock_t, nb: bool) {
218218
let set = nb as libc::c_int;
219-
mkerr_libc(retry(|| unsafe { c::ioctl(fd, c::FIONBIO, &set) }))
219+
mkerr_libc(retry(|| unsafe { c::ioctl(fd, c::FIONBIO, &set) })).unwrap();
220220
}
221221

222222
// nothing needed on unix platforms

src/libstd/sys/unix/pipe.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -235,9 +235,9 @@ impl UnixListener {
235235

236236
_ => {
237237
let (reader, writer) = try!(unsafe { sys::os::pipe() });
238-
try!(set_nonblocking(reader.fd(), true));
239-
try!(set_nonblocking(writer.fd(), true));
240-
try!(set_nonblocking(self.fd(), true));
238+
set_nonblocking(reader.fd(), true);
239+
set_nonblocking(writer.fd(), true);
240+
set_nonblocking(self.fd(), true);
241241
Ok(UnixAcceptor {
242242
inner: Arc::new(AcceptorInner {
243243
listener: self,

src/libstd/sys/unix/process.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -345,8 +345,8 @@ impl Process {
345345
unsafe {
346346
let mut pipes = [0; 2];
347347
assert_eq!(libc::pipe(pipes.as_mut_ptr()), 0);
348-
set_nonblocking(pipes[0], true).ok().unwrap();
349-
set_nonblocking(pipes[1], true).ok().unwrap();
348+
set_nonblocking(pipes[0], true);
349+
set_nonblocking(pipes[1], true);
350350
WRITE_FD = pipes[1];
351351

352352
let mut old: c::sigaction = mem::zeroed();
@@ -362,7 +362,7 @@ impl Process {
362362
fn waitpid_helper(input: libc::c_int,
363363
messages: Receiver<Req>,
364364
(read_fd, old): (libc::c_int, c::sigaction)) {
365-
set_nonblocking(input, true).ok().unwrap();
365+
set_nonblocking(input, true);
366366
let mut set: c::fd_set = unsafe { mem::zeroed() };
367367
let mut tv: libc::timeval;
368368
let mut active = Vec::<(libc::pid_t, Sender<ProcessExit>, u64)>::new();

src/libstd/sys/unix/tcp.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ impl TcpListener {
6767
-1 => Err(last_net_error()),
6868
_ => {
6969
let (reader, writer) = try!(unsafe { sys::os::pipe() });
70-
try!(set_nonblocking(reader.fd(), true));
71-
try!(set_nonblocking(writer.fd(), true));
72-
try!(set_nonblocking(self.fd(), true));
70+
set_nonblocking(reader.fd(), true);
71+
set_nonblocking(writer.fd(), true);
72+
set_nonblocking(self.fd(), true);
7373
Ok(TcpAcceptor {
7474
inner: Arc::new(AcceptorInner {
7575
listener: self,

src/libstd/sys/windows/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,13 +192,13 @@ pub fn wouldblock() -> bool {
192192
err == libc::WSAEWOULDBLOCK as i32
193193
}
194194

195-
pub fn set_nonblocking(fd: sock_t, nb: bool) -> IoResult<()> {
195+
pub fn set_nonblocking(fd: sock_t, nb: bool) {
196196
let mut set = nb as libc::c_ulong;
197-
if unsafe { c::ioctlsocket(fd, c::FIONBIO, &mut set) != 0 } {
197+
(if unsafe { c::ioctlsocket(fd, c::FIONBIO, &mut set) } != 0 {
198198
Err(last_error())
199199
} else {
200200
Ok(())
201-
}
201+
}).unwrap();
202202
}
203203

204204
pub fn init_net() {

src/libstd/sys/windows/net.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ pub type wrlen_t = i32;
2525

2626
pub struct Socket(libc::SOCKET);
2727

28+
/// Checks whether the Windows socket interface has been started already, and
29+
/// if not, starts it.
2830
pub fn init() {
2931
static START: Once = ONCE_INIT;
3032

@@ -38,10 +40,16 @@ pub fn init() {
3840
});
3941
}
4042

43+
/// Returns the last error from the Windows socket interface.
4144
fn last_error() -> io::Error {
4245
io::Error::from_os_error(unsafe { c::WSAGetLastError() })
4346
}
4447

48+
/// Checks if the signed integer is the Windows constant `SOCKET_ERROR` (-1)
49+
/// and if so, returns the last error from the Windows socket interface. . This
50+
/// function must be called before another call to the socket API is made.
51+
///
52+
/// FIXME: generics needed?
4553
pub fn cvt<T: SignedInt>(t: T) -> io::Result<T> {
4654
let one: T = Int::one();
4755
if t == -one {
@@ -51,11 +59,14 @@ pub fn cvt<T: SignedInt>(t: T) -> io::Result<T> {
5159
}
5260
}
5361

62+
/// Provides the functionality of `cvt` for the return values of `getaddrinfo`
63+
/// and similar, meaning that they return an error if the return value is 0.
5464
pub fn cvt_gai(err: c_int) -> io::Result<()> {
5565
if err == 0 { return Ok(()) }
5666
cvt(err).map(|_| ())
5767
}
5868

69+
/// Provides the functionality of `cvt` for a closure.
5970
pub fn cvt_r<T: SignedInt, F>(mut f: F) -> io::Result<T> where F: FnMut() -> T {
6071
cvt(f())
6172
}
@@ -112,7 +123,7 @@ impl Socket {
112123

113124
impl Drop for Socket {
114125
fn drop(&mut self) {
115-
unsafe { let _ = libc::closesocket(self.0); }
126+
unsafe { cvt(libc::closesocket(self.0)).unwrap(); }
116127
}
117128
}
118129

src/libstd/sys/windows/tcp.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ impl TcpAcceptor {
192192
c::WSAEventSelect(socket, events[1], 0)
193193
};
194194
if ret != 0 { return Err(last_net_error()) }
195-
try!(set_nonblocking(socket, false));
195+
set_nonblocking(socket, false);
196196
return Ok(stream)
197197
}
198198
}

0 commit comments

Comments
 (0)