Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement AsFd/AsSocket/etc. conversions #889

Merged
merged 1 commit into from
Oct 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 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 @@
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 @@
}
}

#[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::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 @@
}
}

#[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