Skip to content

Commit

Permalink
Implement AsFd/AsSocket/etc. conversions
Browse files Browse the repository at this point in the history
Generally these should be implemented anywhere the raw versions are.
  • Loading branch information
ids1024 committed Oct 14, 2023
1 parent 55ba7b0 commit 8c7697d
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
36 changes: 33 additions & 3 deletions x11rb/src/rust_connection/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ use rustix::fd::AsFd;
use std::io::{IoSlice, Result};
use std::net::TcpStream;
#[cfg(unix)]
use std::os::unix::io::{AsRawFd, IntoRawFd, RawFd};
use std::os::unix::io::{AsRawFd, BorrowedFd, IntoRawFd, OwnedFd, RawFd};
#[cfg(unix)]
use std::os::unix::net::UnixStream;
#[cfg(windows)]
use std::os::windows::io::{AsRawSocket, IntoRawSocket, RawSocket};
use std::os::windows::io::{
AsRawSocket, AsSocket, BorrowedSocket, IntoRawSocket, OwnedSocket, RawSocket,
};

use crate::utils::RawFdContainer;
use x11rb_protocol::parse_display::ConnectAddress;
Expand Down Expand Up @@ -222,6 +224,7 @@ impl DefaultStream {
Ok((result, peer_addr::local()))
}

#[allow(unused_qualifications)]
fn as_fd(&self) -> rustix::fd::BorrowedFd<'_> {
self.inner.as_fd()
}
Expand All @@ -234,34 +237,61 @@ impl AsRawFd for DefaultStream {
}
}

#[cfg(unix)]
impl AsFd for DefaultStream {
fn as_fd(&self) -> BorrowedFd<'_> {
self.inner.as_fd()
}

Check warning on line 244 in x11rb/src/rust_connection/stream.rs

View check run for this annotation

Codecov / codecov/patch

x11rb/src/rust_connection/stream.rs#L242-L244

Added lines #L242 - L244 were not covered by tests
}

#[cfg(unix)]
impl IntoRawFd for DefaultStream {
fn into_raw_fd(self) -> RawFd {
self.inner.into_raw_fd()
}
}

#[cfg(unix)]
impl From<DefaultStream> for OwnedFd {
fn from(stream: DefaultStream) -> Self {
stream.inner
}

Check warning on line 258 in x11rb/src/rust_connection/stream.rs

View check run for this annotation

Codecov / codecov/patch

x11rb/src/rust_connection/stream.rs#L256-L258

Added lines #L256 - L258 were not covered by tests
}

#[cfg(windows)]
impl AsRawSocket for DefaultStream {
fn as_raw_socket(&self) -> RawSocket {
self.inner.as_raw_socket()
}
}

#[cfg(windows)]
impl AsSocket for DefaultStream {
fn as_socket(&self) -> BorrowedSocket<'_> {
self.inner.as_socket()
}
}

#[cfg(windows)]
impl IntoRawSocket for DefaultStream {
fn into_raw_socket(self) -> RawSocket {
self.inner.into_raw_socket()
}
}

#[cfg(windows)]
impl From<DefaultStream> for OwnedSocket {
fn from(stream: DefaultStream) -> Self {
stream.inner.into()
}
}

#[cfg(unix)]
fn do_write(
stream: &DefaultStream,
bufs: &[IoSlice<'_>],
fds: &mut Vec<RawFdContainer>,
) -> Result<usize> {
use rustix::fd::BorrowedFd;
use rustix::io::Errno;
use rustix::net::{sendmsg, SendAncillaryBuffer, SendAncillaryMessage, SendFlags};

Expand Down
9 changes: 8 additions & 1 deletion x11rb/src/xcb_ffi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::ffi::CStr;
use std::io::{Error as IOError, ErrorKind, IoSlice};
use std::os::raw::c_int;
#[cfg(unix)]
use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, OwnedFd, RawFd};
use std::os::unix::io::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, OwnedFd, RawFd};
use std::ptr::{null, null_mut};
use std::sync::{atomic::Ordering, Mutex};

Expand Down Expand Up @@ -595,6 +595,13 @@ impl AsRawFd for XCBConnection {
}
}

#[cfg(unix)]
impl AsFd for XCBConnection {
fn as_fd(&self) -> BorrowedFd<'_> {
unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) }
}

Check warning on line 602 in x11rb/src/xcb_ffi/mod.rs

View check run for this annotation

Codecov / codecov/patch

x11rb/src/xcb_ffi/mod.rs#L600-L602

Added lines #L600 - L602 were not covered by tests
}

// SAFETY: We provide a valid xcb_connection_t that is valid for as long as required by the trait.
unsafe impl as_raw_xcb_connection::AsRawXcbConnection for XCBConnection {
fn as_raw_xcb_connection(&self) -> *mut as_raw_xcb_connection::xcb_connection_t {
Expand Down

0 comments on commit 8c7697d

Please sign in to comment.