File tree 1 file changed +25
-0
lines changed
1 file changed +25
-0
lines changed Original file line number Diff line number Diff line change @@ -26,6 +26,8 @@ use crate::Socket;
26
26
///
27
27
/// # Examples
28
28
///
29
+ /// Below is an example of converting a [`TcpStream`] into a [`SockRef`].
30
+ ///
29
31
/// ```
30
32
/// use std::net::{TcpStream, SocketAddr};
31
33
///
@@ -57,6 +59,29 @@ use crate::Socket;
57
59
/// # Ok(())
58
60
/// # }
59
61
/// ```
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
+ /// ```
60
85
pub struct SockRef < ' s > {
61
86
/// Because this is a reference we don't own the `Socket`, however `Socket`
62
87
/// closes itself when dropped, so we use `ManuallyDrop` to prevent it from
You can’t perform that action at this time.
0 commit comments