Struct SockRef
pub struct SockRef<'s> { /* private fields */ }
A reference to a Socket that can be used to configure socket types other
than the Socket type itself.
This allows for example a TcpStream, found in the standard library, to
be configured using all the additional methods found in the Socket API.
SockRef can be created from any socket type that implements AsFd
(Unix) or AsSocket (Windows) using the From implementation.
Examples
Below is an example of converting a TcpStream into a SockRef.
use std::net::{TcpStream, SocketAddr};
use socket2::SockRef;
# fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create `TcpStream` from the standard library.
let address: SocketAddr = "127.0.0.1:1234".parse()?;
# let b1 = std::sync::Arc::new(std::sync::Barrier::new(2));
# let b2 = b1.clone();
# let handle = std::thread::spawn(move || {
# let listener = std::net::TcpListener::bind(address).unwrap();
# b2.wait();
# let (stream, _) = listener.accept().unwrap();
# std::thread::sleep(std::time::Duration::from_millis(10));
# drop(stream);
# });
# b1.wait();
let stream = TcpStream::connect(address)?;
// Create a `SockRef`erence to the stream.
let socket_ref = SockRef::from(&stream);
// Use `Socket::set_tcp_nodelay` on the stream.
socket_ref.set_tcp_nodelay(true)?;
drop(socket_ref);
assert_eq!(stream.nodelay()?, true);
# handle.join().unwrap();
# Ok(())
# }
Trait Implementations
impl Debug for SockRef<'_>
fn fmt(&self, f: &mut Formatter<'_>) -> Result
impl<'s> Deref for SockRef<'s>
type Target = Socket;fn deref(&self) -> &Self::Target
impl<'s, S> From<&'s S> for SockRef<'s>
where
S: AsFd,
fn from(socket: &'s S) -> SelfThe caller must ensure
Sis actually a socket.