Skip to content

Commit

Permalink
Fixes after review
Browse files Browse the repository at this point in the history
  • Loading branch information
nikarh authored and Thomasdezeeuw committed Oct 9, 2023
1 parent beeb126 commit f1349f3
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -684,7 +684,8 @@ impl<'addr, 'bufs, 'control> MsgHdrMut<'addr, 'bufs, 'control> {
///
/// Corresponds to setting `msg_name` and `msg_namelen` on Unix and `name`
/// and `namelen` on Windows.
pub fn with_addr(mut self, addr: &SockAddr) -> Self {
#[allow(clippy::needless_pass_by_ref_mut)]
pub fn with_addr(mut self, addr: &'addr mut SockAddr) -> Self {
sys::set_msghdr_name(&mut self.inner, addr);
self
}
Expand Down
2 changes: 1 addition & 1 deletion src/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ impl Socket {
/// `O_NONBLOCK`.
///
/// On Windows it is not possible retrieve the nonblocking mode status.
#[cfg(any(target_os = "vita", all(feature = "all", unix)))]
#[cfg(all(feature = "all", unix))]
#[cfg_attr(docsrs, doc(cfg(all(feature = "all", unix))))]
pub fn nonblocking(&self) -> io::Result<bool> {
sys::nonblocking(self.as_raw())
Expand Down
2 changes: 1 addition & 1 deletion src/sys/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -939,7 +939,7 @@ pub(crate) fn nonblocking(fd: Socket) -> io::Result<bool> {
Ok((file_status_flags & libc::O_NONBLOCK) != 0)
}

#[cfg(target_os = "vita")]
#[cfg(all(feature = "all", target_os = "vita"))]
pub(crate) fn nonblocking(fd: Socket) -> io::Result<bool> {
unsafe {
getsockopt::<Bool>(fd, libc::SOL_SOCKET, libc::SO_NONBLOCK).map(|non_block| non_block != 0)
Expand Down
23 changes: 21 additions & 2 deletions tests/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ fn assert_common_flags(socket: &Socket, expected: bool) {
#[cfg(windows)]
assert_flag_no_inherit(socket, expected);

// Vita does not have process API, so neither SO_NOSIGPIPE nor FD_CLOEXEC are supported on this platform
#[cfg(target_os = "vita")]
{
let _ = socket;
Expand Down Expand Up @@ -293,14 +294,32 @@ fn type_nonblocking() {
#[cfg(unix)]
#[track_caller]
pub fn assert_nonblocking(socket: &Socket, want: bool) {
#[cfg(any(target_os = "vita", all(feature = "all", unix)))]
#[cfg(all(feature = "all", unix))]
assert_eq!(socket.nonblocking().unwrap(), want, "non-blocking option");

#[cfg(all(not(target_os = "vita"), not(all(feature = "all", unix))))]
{
let flags = unsafe { libc::fcntl(socket.as_raw_fd(), libc::F_GETFL) };
assert_eq!(flags & libc::O_NONBLOCK != 0, want, "non-blocking option");
}

#[cfg(all(target_os = "vita", not(feature = "all")))]
{
let mut optval: libc::c_int = 0;
let mut optlen = std::mem::size_of::<libc::c_int>() as libc::socklen_t;

let res = unsafe {
libc::getsockopt(
socket.as_raw_fd(),
libc::SOL_SOCKET,
libc::SO_NONBLOCK,
&mut optval as *mut libc::c_int as _,
&mut optlen,
)
};
assert_eq!(res, 0, "unable to get non-blocing option");
assert_eq!(optval > 0, want, "non-blocking option");
}
}

#[cfg(windows)]
Expand Down Expand Up @@ -871,7 +890,7 @@ fn tcp_keepalive() {

#[cfg(all(
feature = "all",
not(any(windows, target_os = "haiku", target_os = "openbsd",))
not(any(windows, target_os = "haiku", target_os = "openbsd"))
))]
assert_eq!(socket.keepalive_time().unwrap(), Duration::from_secs(200));

Expand Down

0 comments on commit f1349f3

Please sign in to comment.