Inline most raw socket, fd and handle conversions

This commit is contained in:
KaiJewson 2021-04-25 07:39:09 +01:00
parent f7c468fe9a
commit fbc2aadbfc
11 changed files with 76 additions and 0 deletions

View File

@ -63,12 +63,14 @@ pub trait TryIntoRawFd: Sized {
}
impl AsRawFd for net::TcpStream {
#[inline]
fn as_raw_fd(&self) -> RawFd {
*self.as_inner().as_inner().as_inner().as_inner()
}
}
impl AsRawFd for net::TcpListener {
#[inline]
fn as_raw_fd(&self) -> RawFd {
*self.as_inner().as_inner().as_inner().as_inner()
}
@ -87,6 +89,7 @@ pub struct TcpStreamMetadata {
impl FromRawFd for net::TcpStream {
type Metadata = TcpStreamMetadata;
#[inline]
unsafe fn from_raw_fd(fd: RawFd, metadata: Self::Metadata) -> net::TcpStream {
let fd = sys::fd::FileDesc::from_inner(fd);
let socket = sys::net::Socket::from_inner((fd, metadata.local_addr));
@ -105,6 +108,7 @@ pub struct TcpListenerMetadata {
impl FromRawFd for net::TcpListener {
type Metadata = TcpListenerMetadata;
#[inline]
unsafe fn from_raw_fd(fd: RawFd, metadata: Self::Metadata) -> net::TcpListener {
let fd = sys::fd::FileDesc::from_inner(fd);
let socket = sys::net::Socket::from_inner((fd, metadata.local_addr));
@ -113,6 +117,7 @@ unsafe fn from_raw_fd(fd: RawFd, metadata: Self::Metadata) -> net::TcpListener {
}
impl TryIntoRawFd for net::TcpStream {
#[inline]
fn try_into_raw_fd(self) -> Result<RawFd, Self> {
let (socket, peer_addr) = self.into_inner().into_inner();
match socket.try_into_inner() {
@ -126,6 +131,7 @@ fn try_into_raw_fd(self) -> Result<RawFd, Self> {
}
impl TryIntoRawFd for net::TcpListener {
#[inline]
fn try_into_raw_fd(self) -> Result<RawFd, Self> {
match self.into_inner().into_inner().try_into_inner() {
Ok(fd) => Ok(fd.into_inner()),

View File

@ -104,18 +104,21 @@ pub trait IntoRawFd {
#[stable(feature = "raw_fd_reflexive_traits", since = "1.48.0")]
impl AsRawFd for RawFd {
#[inline]
fn as_raw_fd(&self) -> RawFd {
*self
}
}
#[stable(feature = "raw_fd_reflexive_traits", since = "1.48.0")]
impl IntoRawFd for RawFd {
#[inline]
fn into_raw_fd(self) -> RawFd {
self
}
}
#[stable(feature = "raw_fd_reflexive_traits", since = "1.48.0")]
impl FromRawFd for RawFd {
#[inline]
unsafe fn from_raw_fd(fd: RawFd) -> RawFd {
fd
}
@ -123,18 +126,21 @@ unsafe fn from_raw_fd(fd: RawFd) -> RawFd {
#[stable(feature = "rust1", since = "1.0.0")]
impl AsRawFd for fs::File {
#[inline]
fn as_raw_fd(&self) -> RawFd {
self.as_inner().fd().raw()
}
}
#[stable(feature = "from_raw_os", since = "1.1.0")]
impl FromRawFd for fs::File {
#[inline]
unsafe fn from_raw_fd(fd: RawFd) -> fs::File {
fs::File::from_inner(sys::fs::File::from_inner(fd))
}
}
#[stable(feature = "into_raw_os", since = "1.4.0")]
impl IntoRawFd for fs::File {
#[inline]
fn into_raw_fd(self) -> RawFd {
self.into_inner().into_fd().into_raw()
}
@ -142,6 +148,7 @@ fn into_raw_fd(self) -> RawFd {
#[stable(feature = "asraw_stdio", since = "1.21.0")]
impl AsRawFd for io::Stdin {
#[inline]
fn as_raw_fd(&self) -> RawFd {
libc::STDIN_FILENO
}
@ -149,6 +156,7 @@ fn as_raw_fd(&self) -> RawFd {
#[stable(feature = "asraw_stdio", since = "1.21.0")]
impl AsRawFd for io::Stdout {
#[inline]
fn as_raw_fd(&self) -> RawFd {
libc::STDOUT_FILENO
}
@ -156,6 +164,7 @@ fn as_raw_fd(&self) -> RawFd {
#[stable(feature = "asraw_stdio", since = "1.21.0")]
impl AsRawFd for io::Stderr {
#[inline]
fn as_raw_fd(&self) -> RawFd {
libc::STDERR_FILENO
}
@ -163,6 +172,7 @@ fn as_raw_fd(&self) -> RawFd {
#[stable(feature = "asraw_stdio_locks", since = "1.35.0")]
impl<'a> AsRawFd for io::StdinLock<'a> {
#[inline]
fn as_raw_fd(&self) -> RawFd {
libc::STDIN_FILENO
}
@ -170,6 +180,7 @@ fn as_raw_fd(&self) -> RawFd {
#[stable(feature = "asraw_stdio_locks", since = "1.35.0")]
impl<'a> AsRawFd for io::StdoutLock<'a> {
#[inline]
fn as_raw_fd(&self) -> RawFd {
libc::STDOUT_FILENO
}
@ -177,6 +188,7 @@ fn as_raw_fd(&self) -> RawFd {
#[stable(feature = "asraw_stdio_locks", since = "1.35.0")]
impl<'a> AsRawFd for io::StderrLock<'a> {
#[inline]
fn as_raw_fd(&self) -> RawFd {
libc::STDERR_FILENO
}

View File

@ -879,6 +879,7 @@ pub fn peek_from(&self, buf: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
#[stable(feature = "unix_socket", since = "1.10.0")]
impl AsRawFd for UnixDatagram {
#[inline]
fn as_raw_fd(&self) -> RawFd {
*self.0.as_inner()
}
@ -886,6 +887,7 @@ fn as_raw_fd(&self) -> RawFd {
#[stable(feature = "unix_socket", since = "1.10.0")]
impl FromRawFd for UnixDatagram {
#[inline]
unsafe fn from_raw_fd(fd: RawFd) -> UnixDatagram {
UnixDatagram(Socket::from_inner(fd))
}
@ -893,6 +895,7 @@ unsafe fn from_raw_fd(fd: RawFd) -> UnixDatagram {
#[stable(feature = "unix_socket", since = "1.10.0")]
impl IntoRawFd for UnixDatagram {
#[inline]
fn into_raw_fd(self) -> RawFd {
self.0.into_inner()
}

View File

@ -240,6 +240,7 @@ pub fn incoming(&self) -> Incoming<'_> {
#[stable(feature = "unix_socket", since = "1.10.0")]
impl AsRawFd for UnixListener {
#[inline]
fn as_raw_fd(&self) -> RawFd {
*self.0.as_inner()
}
@ -247,6 +248,7 @@ fn as_raw_fd(&self) -> RawFd {
#[stable(feature = "unix_socket", since = "1.10.0")]
impl FromRawFd for UnixListener {
#[inline]
unsafe fn from_raw_fd(fd: RawFd) -> UnixListener {
UnixListener(Socket::from_inner(fd))
}
@ -254,6 +256,7 @@ unsafe fn from_raw_fd(fd: RawFd) -> UnixListener {
#[stable(feature = "unix_socket", since = "1.10.0")]
impl IntoRawFd for UnixListener {
#[inline]
fn into_raw_fd(self) -> RawFd {
self.0.into_inner()
}

View File

@ -6,6 +6,7 @@ macro_rules! impl_as_raw_fd {
($($t:ident)*) => {$(
#[stable(feature = "rust1", since = "1.0.0")]
impl AsRawFd for net::$t {
#[inline]
fn as_raw_fd(&self) -> RawFd {
*self.as_inner().socket().as_inner()
}
@ -18,6 +19,7 @@ macro_rules! impl_from_raw_fd {
($($t:ident)*) => {$(
#[stable(feature = "from_raw_os", since = "1.1.0")]
impl FromRawFd for net::$t {
#[inline]
unsafe fn from_raw_fd(fd: RawFd) -> net::$t {
let socket = sys::net::Socket::from_inner(fd);
net::$t::from_inner(sys_common::net::$t::from_inner(socket))
@ -31,6 +33,7 @@ macro_rules! impl_into_raw_fd {
($($t:ident)*) => {$(
#[stable(feature = "into_raw_os", since = "1.4.0")]
impl IntoRawFd for net::$t {
#[inline]
fn into_raw_fd(self) -> RawFd {
self.into_inner().into_socket().into_inner()
}

View File

@ -654,6 +654,7 @@ fn flush(&mut self) -> io::Result<()> {
#[stable(feature = "unix_socket", since = "1.10.0")]
impl AsRawFd for UnixStream {
#[inline]
fn as_raw_fd(&self) -> RawFd {
*self.0.as_inner()
}
@ -661,6 +662,7 @@ fn as_raw_fd(&self) -> RawFd {
#[stable(feature = "unix_socket", since = "1.10.0")]
impl FromRawFd for UnixStream {
#[inline]
unsafe fn from_raw_fd(fd: RawFd) -> UnixStream {
UnixStream(Socket::from_inner(fd))
}
@ -668,6 +670,7 @@ unsafe fn from_raw_fd(fd: RawFd) -> UnixStream {
#[stable(feature = "unix_socket", since = "1.10.0")]
impl IntoRawFd for UnixStream {
#[inline]
fn into_raw_fd(self) -> RawFd {
self.0.into_inner()
}

View File

@ -274,6 +274,7 @@ fn into_raw(self) -> i32 {
#[stable(feature = "process_extensions", since = "1.2.0")]
impl FromRawFd for process::Stdio {
#[inline]
unsafe fn from_raw_fd(fd: RawFd) -> process::Stdio {
let fd = sys::fd::FileDesc::new(fd);
let io = sys::process::Stdio::Fd(fd);
@ -283,6 +284,7 @@ unsafe fn from_raw_fd(fd: RawFd) -> process::Stdio {
#[stable(feature = "process_extensions", since = "1.2.0")]
impl AsRawFd for process::ChildStdin {
#[inline]
fn as_raw_fd(&self) -> RawFd {
self.as_inner().fd().raw()
}
@ -290,6 +292,7 @@ fn as_raw_fd(&self) -> RawFd {
#[stable(feature = "process_extensions", since = "1.2.0")]
impl AsRawFd for process::ChildStdout {
#[inline]
fn as_raw_fd(&self) -> RawFd {
self.as_inner().fd().raw()
}
@ -297,6 +300,7 @@ fn as_raw_fd(&self) -> RawFd {
#[stable(feature = "process_extensions", since = "1.2.0")]
impl AsRawFd for process::ChildStderr {
#[inline]
fn as_raw_fd(&self) -> RawFd {
self.as_inner().fd().raw()
}
@ -304,6 +308,7 @@ fn as_raw_fd(&self) -> RawFd {
#[stable(feature = "into_raw_os", since = "1.4.0")]
impl IntoRawFd for process::ChildStdin {
#[inline]
fn into_raw_fd(self) -> RawFd {
self.into_inner().into_fd().into_raw()
}
@ -311,6 +316,7 @@ fn into_raw_fd(self) -> RawFd {
#[stable(feature = "into_raw_os", since = "1.4.0")]
impl IntoRawFd for process::ChildStdout {
#[inline]
fn into_raw_fd(self) -> RawFd {
self.into_inner().into_fd().into_raw()
}
@ -318,6 +324,7 @@ fn into_raw_fd(self) -> RawFd {
#[stable(feature = "into_raw_os", since = "1.4.0")]
impl IntoRawFd for process::ChildStderr {
#[inline]
fn into_raw_fd(self) -> RawFd {
self.into_inner().into_fd().into_raw()
}

View File

@ -54,126 +54,147 @@ pub trait IntoRawFd {
#[stable(feature = "raw_fd_reflexive_traits", since = "1.48.0")]
impl AsRawFd for RawFd {
#[inline]
fn as_raw_fd(&self) -> RawFd {
*self
}
}
#[stable(feature = "raw_fd_reflexive_traits", since = "1.48.0")]
impl IntoRawFd for RawFd {
#[inline]
fn into_raw_fd(self) -> RawFd {
self
}
}
#[stable(feature = "raw_fd_reflexive_traits", since = "1.48.0")]
impl FromRawFd for RawFd {
#[inline]
unsafe fn from_raw_fd(fd: RawFd) -> RawFd {
fd
}
}
impl AsRawFd for net::TcpStream {
#[inline]
fn as_raw_fd(&self) -> RawFd {
self.as_inner().fd().as_raw()
}
}
impl FromRawFd for net::TcpStream {
#[inline]
unsafe fn from_raw_fd(fd: RawFd) -> net::TcpStream {
net::TcpStream::from_inner(sys::net::TcpStream::from_inner(fd))
}
}
impl IntoRawFd for net::TcpStream {
#[inline]
fn into_raw_fd(self) -> RawFd {
self.into_inner().into_fd().into_raw()
}
}
impl AsRawFd for net::TcpListener {
#[inline]
fn as_raw_fd(&self) -> RawFd {
self.as_inner().fd().as_raw()
}
}
impl FromRawFd for net::TcpListener {
#[inline]
unsafe fn from_raw_fd(fd: RawFd) -> net::TcpListener {
net::TcpListener::from_inner(sys::net::TcpListener::from_inner(fd))
}
}
impl IntoRawFd for net::TcpListener {
#[inline]
fn into_raw_fd(self) -> RawFd {
self.into_inner().into_fd().into_raw()
}
}
impl AsRawFd for net::UdpSocket {
#[inline]
fn as_raw_fd(&self) -> RawFd {
self.as_inner().fd().as_raw()
}
}
impl FromRawFd for net::UdpSocket {
#[inline]
unsafe fn from_raw_fd(fd: RawFd) -> net::UdpSocket {
net::UdpSocket::from_inner(sys::net::UdpSocket::from_inner(fd))
}
}
impl IntoRawFd for net::UdpSocket {
#[inline]
fn into_raw_fd(self) -> RawFd {
self.into_inner().into_fd().into_raw()
}
}
impl AsRawFd for fs::File {
#[inline]
fn as_raw_fd(&self) -> RawFd {
self.as_inner().fd().as_raw()
}
}
impl FromRawFd for fs::File {
#[inline]
unsafe fn from_raw_fd(fd: RawFd) -> fs::File {
fs::File::from_inner(sys::fs::File::from_inner(fd))
}
}
impl IntoRawFd for fs::File {
#[inline]
fn into_raw_fd(self) -> RawFd {
self.into_inner().into_fd().into_raw()
}
}
impl AsRawFd for io::Stdin {
#[inline]
fn as_raw_fd(&self) -> RawFd {
libc::STDIN_FILENO as RawFd
}
}
impl AsRawFd for io::Stdout {
#[inline]
fn as_raw_fd(&self) -> RawFd {
libc::STDOUT_FILENO as RawFd
}
}
impl AsRawFd for io::Stderr {
#[inline]
fn as_raw_fd(&self) -> RawFd {
libc::STDERR_FILENO as RawFd
}
}
impl<'a> AsRawFd for io::StdinLock<'a> {
#[inline]
fn as_raw_fd(&self) -> RawFd {
libc::STDIN_FILENO as RawFd
}
}
impl<'a> AsRawFd for io::StdoutLock<'a> {
#[inline]
fn as_raw_fd(&self) -> RawFd {
libc::STDOUT_FILENO as RawFd
}
}
impl<'a> AsRawFd for io::StderrLock<'a> {
#[inline]
fn as_raw_fd(&self) -> RawFd {
libc::STDERR_FILENO as RawFd
}

View File

@ -59,6 +59,7 @@ pub trait IntoRawHandle {
#[stable(feature = "rust1", since = "1.0.0")]
impl AsRawHandle for fs::File {
#[inline]
fn as_raw_handle(&self) -> RawHandle {
self.as_inner().handle().raw() as RawHandle
}
@ -108,6 +109,7 @@ fn as_raw_handle(&self) -> RawHandle {
#[stable(feature = "from_raw_os", since = "1.1.0")]
impl FromRawHandle for fs::File {
#[inline]
unsafe fn from_raw_handle(handle: RawHandle) -> fs::File {
let handle = handle as c::HANDLE;
fs::File::from_inner(sys::fs::File::from_inner(handle))
@ -116,6 +118,7 @@ unsafe fn from_raw_handle(handle: RawHandle) -> fs::File {
#[stable(feature = "into_raw_os", since = "1.4.0")]
impl IntoRawHandle for fs::File {
#[inline]
fn into_raw_handle(self) -> RawHandle {
self.into_inner().into_handle().into_raw() as *mut _
}
@ -161,18 +164,21 @@ pub trait IntoRawSocket {
#[stable(feature = "rust1", since = "1.0.0")]
impl AsRawSocket for net::TcpStream {
#[inline]
fn as_raw_socket(&self) -> RawSocket {
*self.as_inner().socket().as_inner()
}
}
#[stable(feature = "rust1", since = "1.0.0")]
impl AsRawSocket for net::TcpListener {
#[inline]
fn as_raw_socket(&self) -> RawSocket {
*self.as_inner().socket().as_inner()
}
}
#[stable(feature = "rust1", since = "1.0.0")]
impl AsRawSocket for net::UdpSocket {
#[inline]
fn as_raw_socket(&self) -> RawSocket {
*self.as_inner().socket().as_inner()
}
@ -180,6 +186,7 @@ fn as_raw_socket(&self) -> RawSocket {
#[stable(feature = "from_raw_os", since = "1.1.0")]
impl FromRawSocket for net::TcpStream {
#[inline]
unsafe fn from_raw_socket(sock: RawSocket) -> net::TcpStream {
let sock = sys::net::Socket::from_inner(sock);
net::TcpStream::from_inner(sys_common::net::TcpStream::from_inner(sock))
@ -187,6 +194,7 @@ unsafe fn from_raw_socket(sock: RawSocket) -> net::TcpStream {
}
#[stable(feature = "from_raw_os", since = "1.1.0")]
impl FromRawSocket for net::TcpListener {
#[inline]
unsafe fn from_raw_socket(sock: RawSocket) -> net::TcpListener {
let sock = sys::net::Socket::from_inner(sock);
net::TcpListener::from_inner(sys_common::net::TcpListener::from_inner(sock))
@ -194,6 +202,7 @@ unsafe fn from_raw_socket(sock: RawSocket) -> net::TcpListener {
}
#[stable(feature = "from_raw_os", since = "1.1.0")]
impl FromRawSocket for net::UdpSocket {
#[inline]
unsafe fn from_raw_socket(sock: RawSocket) -> net::UdpSocket {
let sock = sys::net::Socket::from_inner(sock);
net::UdpSocket::from_inner(sys_common::net::UdpSocket::from_inner(sock))
@ -202,6 +211,7 @@ unsafe fn from_raw_socket(sock: RawSocket) -> net::UdpSocket {
#[stable(feature = "into_raw_os", since = "1.4.0")]
impl IntoRawSocket for net::TcpStream {
#[inline]
fn into_raw_socket(self) -> RawSocket {
self.into_inner().into_socket().into_inner()
}
@ -209,6 +219,7 @@ fn into_raw_socket(self) -> RawSocket {
#[stable(feature = "into_raw_os", since = "1.4.0")]
impl IntoRawSocket for net::TcpListener {
#[inline]
fn into_raw_socket(self) -> RawSocket {
self.into_inner().into_socket().into_inner()
}
@ -216,6 +227,7 @@ fn into_raw_socket(self) -> RawSocket {
#[stable(feature = "into_raw_os", since = "1.4.0")]
impl IntoRawSocket for net::UdpSocket {
#[inline]
fn into_raw_socket(self) -> RawSocket {
self.into_inner().into_socket().into_inner()
}

View File

@ -19,6 +19,7 @@ unsafe fn from_raw_handle(handle: RawHandle) -> process::Stdio {
#[stable(feature = "process_extensions", since = "1.2.0")]
impl AsRawHandle for process::Child {
#[inline]
fn as_raw_handle(&self) -> RawHandle {
self.as_inner().handle().raw() as *mut _
}
@ -33,6 +34,7 @@ fn into_raw_handle(self) -> RawHandle {
#[stable(feature = "process_extensions", since = "1.2.0")]
impl AsRawHandle for process::ChildStdin {
#[inline]
fn as_raw_handle(&self) -> RawHandle {
self.as_inner().handle().raw() as *mut _
}
@ -40,6 +42,7 @@ fn as_raw_handle(&self) -> RawHandle {
#[stable(feature = "process_extensions", since = "1.2.0")]
impl AsRawHandle for process::ChildStdout {
#[inline]
fn as_raw_handle(&self) -> RawHandle {
self.as_inner().handle().raw() as *mut _
}
@ -47,6 +50,7 @@ fn as_raw_handle(&self) -> RawHandle {
#[stable(feature = "process_extensions", since = "1.2.0")]
impl AsRawHandle for process::ChildStderr {
#[inline]
fn as_raw_handle(&self) -> RawHandle {
self.as_inner().handle().raw() as *mut _
}

View File

@ -8,6 +8,7 @@
#[stable(feature = "thread_extensions", since = "1.9.0")]
impl<T> AsRawHandle for thread::JoinHandle<T> {
#[inline]
fn as_raw_handle(&self) -> RawHandle {
self.as_inner().handle().raw() as *mut _
}
@ -15,6 +16,7 @@ fn as_raw_handle(&self) -> RawHandle {
#[stable(feature = "thread_extensions", since = "1.9.0")]
impl<T> IntoRawHandle for thread::JoinHandle<T> {
#[inline]
fn into_raw_handle(self) -> RawHandle {
self.into_inner().into_handle().into_raw() as *mut _
}