@@ -3,9 +3,9 @@ use std::marker::PhantomData;
3
3
use std:: mem:: ManuallyDrop ;
4
4
use std:: ops:: Deref ;
5
5
#[ cfg( unix) ]
6
- use std:: os:: unix:: io:: { AsRawFd , FromRawFd } ;
6
+ use std:: os:: unix:: io:: { AsFd , AsRawFd , FromRawFd } ;
7
7
#[ cfg( windows) ]
8
- use std:: os:: windows:: io:: { AsRawSocket , FromRawSocket } ;
8
+ use std:: os:: windows:: io:: { AsRawSocket , AsSocket , FromRawSocket } ;
9
9
10
10
use crate :: Socket ;
11
11
@@ -15,14 +15,13 @@ use crate::Socket;
15
15
/// This allows for example a [`TcpStream`], found in the standard library, to
16
16
/// be configured using all the additional methods found in the [`Socket`] API.
17
17
///
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.
21
20
///
22
21
/// [`TcpStream`]: std::net::TcpStream
23
22
// 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
26
25
///
27
26
/// # Examples
28
27
///
@@ -59,29 +58,6 @@ use crate::Socket;
59
58
/// # Ok(())
60
59
/// # }
61
60
/// ```
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
- /// ```
85
61
pub struct SockRef < ' s > {
86
62
/// Because this is a reference we don't own the `Socket`, however `Socket`
87
63
/// closes itself when dropped, so we use `ManuallyDrop` to prevent it from
@@ -100,16 +76,16 @@ impl<'s> Deref for SockRef<'s> {
100
76
}
101
77
}
102
78
103
- /// On Windows, a corresponding `From<&impl AsRawSocket >` implementation exists.
79
+ /// On Windows, a corresponding `From<&impl AsSocket >` implementation exists.
104
80
#[ cfg( unix) ]
105
81
#[ cfg_attr( docsrs, doc( cfg( unix) ) ) ]
106
82
impl < ' s , S > From < & ' s S > for SockRef < ' s >
107
83
where
108
- S : AsRawFd ,
84
+ S : AsFd ,
109
85
{
110
86
/// The caller must ensure `S` is actually a socket.
111
87
fn from ( socket : & ' s S ) -> Self {
112
- let fd = socket. as_raw_fd ( ) ;
88
+ let fd = socket. as_fd ( ) . as_raw_fd ( ) ;
113
89
assert ! ( fd >= 0 ) ;
114
90
SockRef {
115
91
socket : ManuallyDrop :: new ( unsafe { Socket :: from_raw_fd ( fd) } ) ,
@@ -118,16 +94,16 @@ where
118
94
}
119
95
}
120
96
121
- /// On Unix, a corresponding `From<&impl AsRawFd >` implementation exists.
97
+ /// On Unix, a corresponding `From<&impl AsFd >` implementation exists.
122
98
#[ cfg( windows) ]
123
99
#[ cfg_attr( docsrs, doc( cfg( windows) ) ) ]
124
100
impl < ' s , S > From < & ' s S > for SockRef < ' s >
125
101
where
126
- S : AsRawSocket ,
102
+ S : AsSocket ,
127
103
{
128
- /// See the `From<&impl AsRawFd >` implementation.
104
+ /// See the `From<&impl AsFd >` implementation.
129
105
fn from ( socket : & ' s S ) -> Self {
130
- let socket = socket. as_raw_socket ( ) ;
106
+ let socket = socket. as_socket ( ) . as_raw_socket ( ) ;
131
107
assert ! ( socket != windows_sys:: Win32 :: Networking :: WinSock :: INVALID_SOCKET as _) ;
132
108
SockRef {
133
109
socket : ManuallyDrop :: new ( unsafe { Socket :: from_raw_socket ( socket) } ) ,
0 commit comments