Skip to content

Commit 990a048

Browse files
committed
Document incorrect usage of SockRef::from
1 parent 313bad2 commit 990a048

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

src/sockref.rs

+25
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ use crate::Socket;
2626
///
2727
/// # Examples
2828
///
29+
/// Below is an example of converting a [`TcpStream`] into a [`SockRef`].
30+
///
2931
/// ```
3032
/// use std::net::{TcpStream, SocketAddr};
3133
///
@@ -57,6 +59,29 @@ use crate::Socket;
5759
/// # Ok(())
5860
/// # }
5961
/// ```
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+
/// ```
6085
pub struct SockRef<'s> {
6186
/// Because this is a reference we don't own the `Socket`, however `Socket`
6287
/// closes itself when dropped, so we use `ManuallyDrop` to prevent it from

0 commit comments

Comments
 (0)