use crate::net::{Shutdown, SocketAddr};
#[cfg(unix)]
use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
#[cfg(windows)]
use std::os::windows::io::{AsRawHandle, FromRawHandle, IntoRawHandle, RawHandle};
use std::{io, net, time::Duration};
pub struct TcpStream {
std: net::TcpStream,
}
impl TcpStream {
#[inline]
pub fn from_std(std: net::TcpStream) -> Self {
Self { std }
}
#[inline]
pub fn local_addr(&self) -> io::Result<SocketAddr> {
self.std.local_addr()
}
#[inline]
pub fn shutdown(&self, how: Shutdown) -> io::Result<()> {
self.std.shutdown(how)
}
#[inline]
pub fn try_clone(&self) -> io::Result<Self> {
Ok(Self::from_std(self.std.try_clone()?))
}
#[inline]
pub fn set_read_timeout(&self, dur: Option<Duration>) -> io::Result<()> {
self.std.set_read_timeout(dur)
}
#[inline]
pub fn set_write_timeout(&self, dur: Option<Duration>) -> io::Result<()> {
self.std.set_write_timeout(dur)
}
#[inline]
pub fn read_timeout(&self) -> io::Result<Option<Duration>> {
self.std.read_timeout()
}
#[inline]
pub fn write_timeout(&self) -> io::Result<Option<Duration>> {
self.std.write_timeout()
}
#[inline]
pub fn peek(&self, buf: &mut [u8]) -> io::Result<usize> {
self.std.peek(buf)
}
#[inline]
pub fn set_nodelay(&self, nodelay: bool) -> io::Result<()> {
self.std.set_nodelay(nodelay)
}
#[inline]
pub fn nodelay(&self) -> io::Result<bool> {
self.std.nodelay()
}
#[inline]
pub fn set_ttl(&self, ttl: u32) -> io::Result<()> {
self.std.set_ttl(ttl)
}
#[inline]
pub fn ttl(&self) -> io::Result<u32> {
self.std.ttl()
}
#[inline]
pub fn take_error(&self) -> io::Result<Option<io::Error>> {
self.std.take_error()
}
#[inline]
pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
self.std.set_nonblocking(nonblocking)
}
}
#[cfg(unix)]
impl FromRawFd for TcpStream {
#[inline]
unsafe fn from_raw_fd(fd: RawFd) -> Self {
Self::from_std(net::TcpStream::from_raw_fd(fd))
}
}
#[cfg(windows)]
impl FromRawSocket for TcpStream {
#[inline]
unsafe fn from_raw_socket(socket: RawSocket) -> Self {
Self::from_std(net::TcpStream::from_raw_socket(handle))
}
}
#[cfg(unix)]
impl AsRawFd for TcpStream {
#[inline]
fn as_raw_fd(&self) -> RawFd {
self.std.as_raw_fd()
}
}
#[cfg(windows)]
impl AsRawSocket for TcpStream {
#[inline]
fn as_raw_socket(&self) -> RawSocket {
self.std.as_raw_socket()
}
}
#[cfg(unix)]
impl IntoRawFd for TcpStream {
#[inline]
fn into_raw_fd(self) -> RawFd {
self.std.into_raw_fd()
}
}
#[cfg(windows)]
impl IntoRawHandle for TcpStream {
#[inline]
fn into_raw_handle(self) -> RawHandle {
self.std.into_raw_handle()
}
}
impl io::Read for TcpStream {
#[inline]
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
self.std.read(buf)
}
#[inline]
fn read_vectored(&mut self, bufs: &mut [io::IoSliceMut]) -> io::Result<usize> {
self.std.read_vectored(bufs)
}
#[inline]
fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> {
self.std.read_exact(buf)
}
#[inline]
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> {
self.std.read_to_end(buf)
}
#[inline]
fn read_to_string(&mut self, buf: &mut String) -> io::Result<usize> {
self.std.read_to_string(buf)
}
}
impl io::Write for TcpStream {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.std.write(buf)
}
fn flush(&mut self) -> io::Result<()> {
self.std.flush()
}
fn write_vectored(&mut self, bufs: &[io::IoSlice]) -> io::Result<usize> {
self.std.write_vectored(bufs)
}
fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
self.std.write_all(buf)
}
}