Skip to content

Commit a18e124

Browse files
committed
Auto merge of #32510 - nodakai:libstd-sys-net-error-check, r=alexcrichton
libstd/sys/*/net: clean up API error checks. 1. Slightly improve `cvt_gai()` and `cvt()`. 2. Remove now redundant `cvt_r()`.
2 parents 68de28b + bf94aef commit a18e124

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

src/libstd/sys/windows/net.rs

+11-10
Original file line numberDiff line numberDiff line change
@@ -61,27 +61,28 @@ fn last_error() -> io::Error {
6161
}
6262

6363
/// Checks if the signed integer is the Windows constant `SOCKET_ERROR` (-1)
64-
/// and if so, returns the last error from the Windows socket interface. . This
64+
/// and if so, returns the last error from the Windows socket interface. This
6565
/// function must be called before another call to the socket API is made.
66-
pub fn cvt<T: One + Neg<Output=T> + PartialEq>(t: T) -> io::Result<T> {
67-
let one: T = T::one();
68-
if t == -one {
66+
pub fn cvt<T: One + PartialEq + Neg<Output=T>>(t: T) -> io::Result<T> {
67+
if t == -T::one() {
6968
Err(last_error())
7069
} else {
7170
Ok(t)
7271
}
7372
}
7473

75-
/// Provides the functionality of `cvt` for the return values of `getaddrinfo`
76-
/// and similar, meaning that they return an error if the return value is 0.
74+
/// A variant of `cvt` for `getaddrinfo` which return 0 for a success.
7775
pub fn cvt_gai(err: c_int) -> io::Result<()> {
78-
if err == 0 { return Ok(()) }
79-
cvt(err).map(|_| ())
76+
if err == 0 {
77+
Ok(())
78+
} else {
79+
Err(last_error())
80+
}
8081
}
8182

82-
/// Provides the functionality of `cvt` for a closure.
83+
/// Just to provide the same interface as sys/unix/net.rs
8384
pub fn cvt_r<T, F>(mut f: F) -> io::Result<T>
84-
where F: FnMut() -> T, T: One + Neg<Output=T> + PartialEq
85+
where T: One + PartialEq + Neg<Output=T>, F: FnMut() -> T
8586
{
8687
cvt(f())
8788
}

0 commit comments

Comments
 (0)