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

Take OwnedFd instead of RawFd in new_from_fd #45

Merged
merged 1 commit into from
Oct 1, 2023
Merged
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
6 changes: 3 additions & 3 deletions src/xkb/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::xkb::ffi::*;
#[cfg(feature = "wayland")]
use memmap2::MmapOptions;
#[cfg(feature = "wayland")]
use std::os::unix::io::{FromRawFd, RawFd};
use std::os::unix::io::OwnedFd;

use libc::{self, c_char, c_int, c_uint};
use std::borrow::Borrow;
Expand Down Expand Up @@ -729,15 +729,15 @@ impl Keymap {
#[allow(clippy::missing_panics_doc)]
pub unsafe fn new_from_fd(
context: &Context,
fd: RawFd,
fd: OwnedFd,
size: usize,
format: KeymapFormat,
flags: KeymapCompileFlags,
) -> std::io::Result<Option<Keymap>> {
let map = MmapOptions::new()
.len(size as usize)
// Starting in version 7 of the wl_keyboard protocol, the keymap must be mapped using MAP_PRIVATE.
.map_copy_read_only(&fs::File::from_raw_fd(fd))?;
.map_copy_read_only(&fs::File::from(fd))?;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why convert to a file? &fd works just fine.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just preserved the behavior it had before, with a type that communicates it.

It looks like map_copy_read_only in memmap2 used to take an &File, so that's probably why a conversion was used here. But now uses a trait MmapAsRawDesc, which works of RawFd and references to types implementing AsRawFd.

So this change works. It also should be possible to accepted a BorrowedFd. (The caller needs to ensure the data isn't mutated while mapped for safety regardless.)

let ptr =
xkb_keymap_new_from_buffer(context.ptr, map.as_ptr().cast(), size - 1, format, flags);
if ptr.is_null() {
Expand Down
Loading