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

Replace nix with rustix #815

Merged
merged 8 commits into from
Jul 30, 2023
Merged
Show file tree
Hide file tree
Changes from 6 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
4 changes: 1 addition & 3 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -160,15 +160,13 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@1.56.0
- uses: dtolnay/rust-toolchain@1.63.0
notgull marked this conversation as resolved.
Show resolved Hide resolved

# build
- name: cargo check x11rb-protocol with all features
run: cargo build --package x11rb-protocol --verbose --lib --all-features
- name: cargo check x11rb with all features
run: cargo build --package x11rb --verbose --lib --all-features
- name: Pin last log release supporting our msrv of Rust 1.56
run: cargo update --package log --precise 0.4.18
- name: cargo check x11rb-async with all features
run: cargo build --package x11rb-async --verbose --lib --all-features

Expand Down
2 changes: 2 additions & 0 deletions generator/src/generator/namespace/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1518,6 +1518,8 @@ impl<'ns, 'c> NamespaceGenerator<'ns, 'c> {
derives.ord = false;
notgull marked this conversation as resolved.
Show resolved Hide resolved
derives.hash = false;
derives.default_ = false;
derives.eq = false;
derives.partial_eq = false;
}
xcbdefs::FieldDef::Expr(_) => {}
xcbdefs::FieldDef::VirtualLen(_) => {}
Expand Down
8 changes: 5 additions & 3 deletions x11rb-async/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ authors = [
repository = "https://github.com/psychon/x11rb"
readme = "../README.md"
edition = "2021"
rust-version = "1.56"
rust-version = "1.63"
license = "MIT OR Apache-2.0"
keywords = ["xcb", "X11", "async"]

Expand All @@ -24,9 +24,10 @@ tracing = { version = "0.1", default-features = false }
x11rb = { version = "0.12.0", path = "../x11rb", default-features = false }
x11rb-protocol = { version = "0.12.0", path = "../x11rb-protocol" }

[target.'cfg(unix)'.dev-dependencies.nix]
version = "0.26"
[target.'cfg(unix)'.dev-dependencies.rustix]
version = "0.38"
default-features = false
features = ["pipe"]

[features]
# Enable this feature to enable all the X11 extensions
Expand Down Expand Up @@ -98,6 +99,7 @@ all-features = true

[dev-dependencies]
async-executor = "1.5.0"
libc = "0.2.147"
tracing-subscriber = { version = "0.3", features = ["env-filter"] }

[[example]]
Expand Down
2 changes: 1 addition & 1 deletion x11rb-async/examples/shared_memory_async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::os::unix::io::AsRawFd;
use std::ptr::null_mut;

use async_executor::LocalExecutor;
use nix::libc::{mmap, MAP_FAILED, MAP_SHARED, PROT_READ, PROT_WRITE};
use libc::{mmap, MAP_FAILED, MAP_SHARED, PROT_READ, PROT_WRITE};

use x11rb_async::connection::Connection;
use x11rb_async::errors::{ConnectionError, ReplyError, ReplyOrIdError};
Expand Down
4 changes: 2 additions & 2 deletions x11rb-async/tests/connection_regression_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ fn retry_for_left_over_fds() {
// Right now that error is WriteZero. That is still better than no error at all.

let fds = {
let (fd0, fd1) = nix::unistd::pipe().unwrap();
vec![RawFdContainer::new(fd0), RawFdContainer::new(fd1)]
let (fd0, fd1) = rustix::pipe::pipe().unwrap();
vec![fd0, fd1]
};

// Send a large request. This should be larger than the write buffer size, which is 16384 bytes.
Expand Down
10 changes: 5 additions & 5 deletions x11rb-protocol/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ authors = [
repository = "https://github.com/psychon/x11rb"
readme = "../README.md"
edition = "2021"
rust-version = "1.56"
rust-version = "1.63"
license = "MIT OR Apache-2.0"
keywords = ["xcb", "X11"]

Expand All @@ -20,15 +20,15 @@ serde = { version = "1", features = ["derive"], optional = true }
[dev-dependencies]
criterion = "0.4"

[target.'cfg(unix)'.dependencies.nix]
version = "0.26"
[target.'cfg(unix)'.dependencies.rustix]
version = "0.38"
default-features = false
features = ["fs"]
features = ["std"]
psychon marked this conversation as resolved.
Show resolved Hide resolved
optional = true

[features]
default = ["std"]
std = ["nix"]
std = ["rustix"]

# Enable utility functions in `x11rb::resource_manager` for querying the
# resource databases.
Expand Down
14 changes: 7 additions & 7 deletions x11rb-protocol/src/protocol/dri3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ impl crate::x11_utils::ReplyFDsRequest for OpenRequest {
type Reply = OpenReply;
}

#[derive(Debug, PartialEq, Eq)]
#[derive(Debug)]
pub struct OpenReply {
pub nfd: u8,
pub sequence: u16,
Expand Down Expand Up @@ -308,7 +308,7 @@ impl Serialize for OpenReply {

/// Opcode for the PixmapFromBuffer request
pub const PIXMAP_FROM_BUFFER_REQUEST: u8 = 2;
#[derive(Debug, PartialEq, Eq)]
#[derive(Debug)]
pub struct PixmapFromBufferRequest {
pub pixmap: xproto::Pixmap,
pub drawable: xproto::Drawable,
Expand Down Expand Up @@ -460,7 +460,7 @@ impl crate::x11_utils::ReplyFDsRequest for BufferFromPixmapRequest {
type Reply = BufferFromPixmapReply;
}

#[derive(Debug, PartialEq, Eq)]
#[derive(Debug)]
pub struct BufferFromPixmapReply {
pub nfd: u8,
pub sequence: u16,
Expand Down Expand Up @@ -566,7 +566,7 @@ impl Serialize for BufferFromPixmapReply {

/// Opcode for the FenceFromFD request
pub const FENCE_FROM_FD_REQUEST: u8 = 4;
#[derive(Debug, PartialEq, Eq)]
#[derive(Debug)]
pub struct FenceFromFDRequest {
pub drawable: xproto::Drawable,
pub fence: u32,
Expand Down Expand Up @@ -699,7 +699,7 @@ impl crate::x11_utils::ReplyFDsRequest for FDFromFenceRequest {
type Reply = FDFromFenceReply;
}

#[derive(Debug, PartialEq, Eq)]
#[derive(Debug)]
pub struct FDFromFenceReply {
pub nfd: u8,
pub sequence: u16,
Expand Down Expand Up @@ -930,7 +930,7 @@ impl GetSupportedModifiersReply {

/// Opcode for the PixmapFromBuffers request
pub const PIXMAP_FROM_BUFFERS_REQUEST: u8 = 7;
#[derive(Debug, PartialEq, Eq)]
#[derive(Debug)]
pub struct PixmapFromBuffersRequest {
pub pixmap: xproto::Pixmap,
pub window: xproto::Window,
Expand Down Expand Up @@ -1157,7 +1157,7 @@ impl crate::x11_utils::ReplyFDsRequest for BuffersFromPixmapRequest {
type Reply = BuffersFromPixmapReply;
}

#[derive(Debug, PartialEq, Eq)]
#[derive(Debug)]
pub struct BuffersFromPixmapReply {
pub sequence: u16,
pub length: u32,
Expand Down
2 changes: 1 addition & 1 deletion x11rb-protocol/src/protocol/randr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6896,7 +6896,7 @@ impl<'input> crate::x11_utils::ReplyFDsRequest for CreateLeaseRequest<'input> {
type Reply = CreateLeaseReply;
}

#[derive(Debug, PartialEq, Eq)]
#[derive(Debug)]
pub struct CreateLeaseReply {
pub nfd: u8,
pub sequence: u16,
Expand Down
4 changes: 2 additions & 2 deletions x11rb-protocol/src/protocol/shm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -997,7 +997,7 @@ pub const ATTACH_FD_REQUEST: u8 = 6;
/// * `shmseg` - A shared memory segment ID created with xcb_generate_id().
/// * `shm_fd` - The file descriptor the server should mmap().
/// * `read_only` - True if the segment shall be mapped read only by the X11 server, otherwise false.
#[derive(Debug, PartialEq, Eq)]
#[derive(Debug)]
pub struct AttachFdRequest {
pub shmseg: Seg,
pub shm_fd: RawFdContainer,
Expand Down Expand Up @@ -1149,7 +1149,7 @@ impl crate::x11_utils::ReplyFDsRequest for CreateSegmentRequest {
/// # Fields
///
/// * `nfd` - The number of file descriptors sent by the server. Will always be 1.
#[derive(Debug, PartialEq, Eq)]
#[derive(Debug)]
pub struct CreateSegmentReply {
pub nfd: u8,
pub sequence: u16,
Expand Down
61 changes: 2 additions & 59 deletions x11rb-protocol/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,70 +11,13 @@

#[cfg(all(feature = "std", unix))]
mod raw_fd_container {
use std::mem::forget;
use std::os::unix::io::{AsRawFd, IntoRawFd, RawFd};
use std::os::unix::io::OwnedFd;

/// A simple wrapper around RawFd that closes the fd on drop.
///
/// On non-unix systems, this type is empty and does not provide
notgull marked this conversation as resolved.
Show resolved Hide resolved
/// any method.
#[derive(Debug, Hash, PartialEq, Eq)]
pub struct RawFdContainer(RawFd);

impl Drop for RawFdContainer {
fn drop(&mut self) {
let _ = nix::unistd::close(self.0);
}
}

impl RawFdContainer {
/// Create a new `RawFdContainer` for the given `RawFd`.
///
/// The `RawFdContainer` takes ownership of the `RawFd` and closes it on drop.
pub fn new(fd: RawFd) -> Self {
RawFdContainer(fd)
}

/// Tries to clone the `RawFdContainer` creating a new FD
/// with `dup`. The new `RawFdContainer` will take ownership
/// of the `dup`ed version, whereas the original `RawFdContainer`
/// will keep the ownership of its FD.
pub fn try_clone(&self) -> Result<Self, std::io::Error> {
Ok(Self::new(nix::unistd::dup(self.0)?))
}

/// Get the `RawFd` out of this `RawFdContainer`.
///
/// This function would be an implementation of `IntoRawFd` if that were possible. However, it
/// causes a conflict with an `impl` from libcore...
pub fn into_raw_fd(self) -> RawFd {
let fd = self.0;
forget(self);
fd
}

/// Consumes the `RawFdContainer` and closes the wrapped FD with
/// the `close` system call.
///
/// This is similar to dropping the `RawFdContainer`, but it allows
/// the caller to handle errors.
pub fn close(self) -> Result<(), std::io::Error> {
let fd = self.into_raw_fd();
nix::unistd::close(fd).map_err(|e| e.into())
}
}

impl<T: IntoRawFd> From<T> for RawFdContainer {
notgull marked this conversation as resolved.
Show resolved Hide resolved
fn from(fd: T) -> Self {
Self::new(fd.into_raw_fd())
}
}

impl AsRawFd for RawFdContainer {
fn as_raw_fd(&self) -> RawFd {
self.0
}
}
pub type RawFdContainer = OwnedFd;
}

#[cfg(not(all(feature = "std", unix)))]
notgull marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
15 changes: 2 additions & 13 deletions x11rb/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ authors = [
repository = "https://github.com/psychon/x11rb"
readme = "../README.md"
edition = "2021"
rust-version = "1.56"
rust-version = "1.63"
license = "MIT OR Apache-2.0"
keywords = ["xcb", "X11"]

Expand All @@ -22,18 +22,7 @@ once_cell = { version = "1.17", optional = true }
gethostname = "0.3.0"
as-raw-xcb-connection = { version = "1.0", optional = true }
tracing = { version = "0.1", optional = true, default-features = false }

[target.'cfg(unix)'.dependencies.nix]
version = "0.26"
default-features = false
features = ["socket", "uio", "poll"]

[target.'cfg(windows)'.dependencies]
winapi-wsapoll = "0.1.1"

[target.'cfg(windows)'.dependencies.winapi]
version = "0.3"
features = ["winsock2"]
rustix = { version = "0.38", default-features = false, features = ["std", "event", "fs", "net"] }

[dev-dependencies]
polling = "2.8.0"
Expand Down
4 changes: 2 additions & 2 deletions x11rb/examples/shared_memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ extern crate x11rb;

use std::fs::{remove_file, File, OpenOptions};
use std::io::{Result as IOResult, Write};
#[cfg(unix)]
use std::os::unix::io::AsRawFd;
use std::ptr::null_mut;

use libc::{mmap, MAP_FAILED, MAP_SHARED, PROT_READ, PROT_WRITE};
use rustix::fd::OwnedFd;

use x11rb::connection::Connection;
use x11rb::errors::{ConnectionError, ReplyError, ReplyOrIdError};
Expand Down Expand Up @@ -100,7 +100,7 @@ fn make_file() -> IOResult<File> {

fn send_fd<C: Connection>(conn: &C, screen_num: usize, file: File) -> Result<(), ReplyOrIdError> {
let shmseg = conn.generate_id()?;
conn.shm_attach_fd(shmseg, file, false)?;
conn.shm_attach_fd(shmseg, OwnedFd::from(file), false)?;

use_shared_mem(conn, screen_num, shmseg)?;

Expand Down
Loading