Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/smol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub struct SmolSocket(Async<Socket>);
impl FromRawFd for SmolSocket {
unsafe fn from_raw_fd(fd: RawFd) -> Self {
let socket = Socket::from_raw_fd(fd);
socket.set_non_blocking(true).unwrap();
socket.set_nonblocking(true).unwrap();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The call to socket.set_nonblocking(true) is redundant because async_io::Async::new() already sets the file descriptor to non-blocking mode. Removing this line also eliminates a potential panic from the use of .unwrap().

SmolSocket(Async::new(socket).unwrap())
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,10 @@ impl Socket {
// when building with --features smol we don't need this
#[allow(dead_code)]
/// Make this socket non-blocking
pub fn set_non_blocking(&self, non_blocking: bool) -> Result<()> {
let mut non_blocking = non_blocking as libc::c_int;
pub fn set_nonblocking(&self, nonblocking: bool) -> Result<()> {
let mut nonblocking = nonblocking as libc::c_int;
let res = unsafe {
libc::ioctl(self.as_raw_fd(), libc::FIONBIO, &mut non_blocking)
libc::ioctl(self.as_raw_fd(), libc::FIONBIO, &mut nonblocking)
Comment on lines +141 to +143

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

To improve clarity and avoid shadowing the nonblocking parameter, consider using a different name for this variable, for example mode. This aligns with general Rust API guidelines that discourage shadowing except in specific cases.

Suggested change
let mut nonblocking = nonblocking as libc::c_int;
let res = unsafe {
libc::ioctl(self.as_raw_fd(), libc::FIONBIO, &mut non_blocking)
libc::ioctl(self.as_raw_fd(), libc::FIONBIO, &mut nonblocking)
let mut mode = nonblocking as libc::c_int;
let res = unsafe {
libc::ioctl(self.as_raw_fd(), libc::FIONBIO, &mut mode)

};
if res < 0 {
return Err(Error::last_os_error());
Expand Down Expand Up @@ -689,10 +689,10 @@ mod test {
}

#[test]
fn set_non_blocking() {
fn set_nonblocking() {
let sock = Socket::new(NETLINK_ROUTE).unwrap();
sock.set_non_blocking(true).unwrap();
sock.set_non_blocking(false).unwrap();
sock.set_nonblocking(true).unwrap();
sock.set_nonblocking(false).unwrap();
}

#[test]
Expand Down
4 changes: 2 additions & 2 deletions src/tokio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub struct TokioSocket(AsyncFd<Socket>);
impl FromRawFd for TokioSocket {
unsafe fn from_raw_fd(fd: RawFd) -> Self {
let socket = Socket::from_raw_fd(fd);
socket.set_non_blocking(true).unwrap();
socket.set_nonblocking(true).unwrap();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The call to socket.set_nonblocking(true) is redundant because tokio::io::unix::AsyncFd::new() already sets the file descriptor to non-blocking mode. Removing this line also eliminates a potential panic from the use of .unwrap().

TokioSocket(AsyncFd::new(socket).unwrap())
}
}
Expand All @@ -41,7 +41,7 @@ impl AsyncSocket for TokioSocket {

fn new(protocol: isize) -> io::Result<Self> {
let socket = Socket::new(protocol)?;
socket.set_non_blocking(true)?;
socket.set_nonblocking(true)?;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This call to socket.set_nonblocking(true) is redundant. The AsyncFd::new(socket)? call on the next line already sets the socket to non-blocking mode, so this line can be safely removed.

Ok(Self(AsyncFd::new(socket)?))
}

Expand Down