Skip to content

Commit eaa6300

Browse files
committed
Use I/O safety traits for SockRef
Replacing AsRawFd with AsFd and AsRawSocket with AsSocket.
1 parent b3e1ab1 commit eaa6300

File tree

1 file changed

+13
-37
lines changed

1 file changed

+13
-37
lines changed

src/sockref.rs

+13-37
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ use std::marker::PhantomData;
33
use std::mem::ManuallyDrop;
44
use std::ops::Deref;
55
#[cfg(unix)]
6-
use std::os::unix::io::{AsRawFd, FromRawFd};
6+
use std::os::unix::io::{AsFd, AsRawFd, FromRawFd};
77
#[cfg(windows)]
8-
use std::os::windows::io::{AsRawSocket, FromRawSocket};
8+
use std::os::windows::io::{AsRawSocket, AsSocket, FromRawSocket};
99

1010
use crate::Socket;
1111

@@ -15,14 +15,13 @@ use crate::Socket;
1515
/// This allows for example a [`TcpStream`], found in the standard library, to
1616
/// be configured using all the additional methods found in the [`Socket`] API.
1717
///
18-
/// `SockRef` can be created from any socket type that implements [`AsRawFd`]
19-
/// (Unix) or [`AsRawSocket`] (Windows) using the [`From`] implementation, but
20-
/// the caller must ensure the file descriptor/socket is a valid.
18+
/// `SockRef` can be created from any socket type that implements [`AsFd`]
19+
/// (Unix) or [`AsSocket`] (Windows) using the [`From`] implementation.
2120
///
2221
/// [`TcpStream`]: std::net::TcpStream
2322
// Don't use intra-doc links because they won't build on every platform.
24-
/// [`AsRawFd`]: https://doc.rust-lang.org/stable/std/os/unix/io/trait.AsRawFd.html
25-
/// [`AsRawSocket`]: https://doc.rust-lang.org/stable/std/os/windows/io/trait.AsRawSocket.html
23+
/// [`AsFd`]: https://doc.rust-lang.org/stable/std/os/unix/io/trait.AsFd.html
24+
/// [`AsSocket`]: https://doc.rust-lang.org/stable/std/os/windows/io/trait.AsSocket.html
2625
///
2726
/// # Examples
2827
///
@@ -59,29 +58,6 @@ use crate::Socket;
5958
/// # Ok(())
6059
/// # }
6160
/// ```
62-
///
63-
/// Below is an example of **incorrect usage** of `SockRef::from`, which is
64-
/// currently possible (but not intended and will be fixed in future versions).
65-
///
66-
/// ```compile_fail
67-
/// use socket2::SockRef;
68-
///
69-
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
70-
/// /// THIS USAGE IS NOT VALID!
71-
/// let socket_ref = SockRef::from(&123);
72-
/// // The above line is overseen possibility when using `SockRef::from`, it
73-
/// // uses the `RawFd` (on Unix), which is a type alias for `c_int`/`i32`,
74-
/// // which implements `AsRawFd`. However it may be clear that this usage is
75-
/// // invalid as it doesn't guarantee that `123` is a valid file descriptor.
76-
///
77-
/// // Using `Socket::set_nodelay` now will call it on a file descriptor we
78-
/// // don't own! We don't even not if the file descriptor is valid or a socket.
79-
/// socket_ref.set_nodelay(true)?;
80-
/// drop(socket_ref);
81-
/// # Ok(())
82-
/// # }
83-
/// # DO_NOT_COMPILE
84-
/// ```
8561
pub struct SockRef<'s> {
8662
/// Because this is a reference we don't own the `Socket`, however `Socket`
8763
/// closes itself when dropped, so we use `ManuallyDrop` to prevent it from
@@ -100,16 +76,16 @@ impl<'s> Deref for SockRef<'s> {
10076
}
10177
}
10278

103-
/// On Windows, a corresponding `From<&impl AsRawSocket>` implementation exists.
79+
/// On Windows, a corresponding `From<&impl AsSocket>` implementation exists.
10480
#[cfg(unix)]
10581
#[cfg_attr(docsrs, doc(cfg(unix)))]
10682
impl<'s, S> From<&'s S> for SockRef<'s>
10783
where
108-
S: AsRawFd,
84+
S: AsFd,
10985
{
11086
/// The caller must ensure `S` is actually a socket.
11187
fn from(socket: &'s S) -> Self {
112-
let fd = socket.as_raw_fd();
88+
let fd = socket.as_fd().as_raw_fd();
11389
assert!(fd >= 0);
11490
SockRef {
11591
socket: ManuallyDrop::new(unsafe { Socket::from_raw_fd(fd) }),
@@ -118,16 +94,16 @@ where
11894
}
11995
}
12096

121-
/// On Unix, a corresponding `From<&impl AsRawFd>` implementation exists.
97+
/// On Unix, a corresponding `From<&impl AsFd>` implementation exists.
12298
#[cfg(windows)]
12399
#[cfg_attr(docsrs, doc(cfg(windows)))]
124100
impl<'s, S> From<&'s S> for SockRef<'s>
125101
where
126-
S: AsRawSocket,
102+
S: AsSocket,
127103
{
128-
/// See the `From<&impl AsRawFd>` implementation.
104+
/// See the `From<&impl AsFd>` implementation.
129105
fn from(socket: &'s S) -> Self {
130-
let socket = socket.as_raw_socket();
106+
let socket = socket.as_socket().as_raw_socket();
131107
assert!(socket != windows_sys::Win32::Networking::WinSock::INVALID_SOCKET as _);
132108
SockRef {
133109
socket: ManuallyDrop::new(unsafe { Socket::from_raw_socket(socket) }),

0 commit comments

Comments
 (0)