Skip to content

Commit

Permalink
feat: impl AsFd for EvdevHandle and UInputHandle
Browse files Browse the repository at this point in the history
  • Loading branch information
arcnmx committed Aug 19, 2024
1 parent 87d62f6 commit e888de0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/evdev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 4,7 @@ use std::{io, fs};
use std::mem::{MaybeUninit, size_of};
use std::slice::from_raw_parts_mut;
use std::os::unix::io::{RawFd, AsRawFd, IntoRawFd, FromRawFd};
use std::os::fd::{AsFd, BorrowedFd};
use nix;
use crate::sys;
use crate::{
Expand Down Expand Up @@ -41,6 42,14 @@ impl<F> EvdevHandle<F> {
}
}

impl<F: AsRawFd> AsFd for EvdevHandle<F> {
fn as_fd<'a>(&'a self) -> BorrowedFd<'a> {
unsafe {
BorrowedFd::borrow_raw(self.fd())
}
}
}

impl<F: AsRawFd> AsRawFd for EvdevHandle<F> {
fn as_raw_fd(&self) -> RawFd {
self.fd()
Expand Down
11 changes: 10 additions & 1 deletion src/uinput.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 4,7 @@
use std::{io, fs, ptr};
use std::os::unix::io::{RawFd, AsRawFd, IntoRawFd, FromRawFd};
use std::os::unix::ffi::OsStrExt;
use std::os::fd::{AsFd, BorrowedFd};
use std::os::raw::c_char;
use std::mem::{MaybeUninit, size_of};
use std::path::{Path, PathBuf};
Expand All @@ -12,7 13,7 @@ use std::ffi::{OsStr, OsString, CStr};
use crate::sys;
use nix;
use crate::{Key, InputId, AbsoluteInfoSetup, kinds};
use crate::macros::{convert_error};
use crate::macros::convert_error;

pub use crate::sys::{UINPUT_MAX_NAME_SIZE, UINPUT_VERSION};

Expand Down Expand Up @@ -54,6 55,14 @@ impl<F> UInputHandle<F> {
}
}

impl<F: AsRawFd> AsFd for UInputHandle<F> {
fn as_fd<'a>(&'a self) -> BorrowedFd<'a> {
unsafe {
BorrowedFd::borrow_raw(self.fd())
}
}
}

impl<F: IntoRawFd> IntoRawFd for UInputHandle<F> {
fn into_raw_fd(self) -> RawFd {
self.0.into_raw_fd()
Expand Down

0 comments on commit e888de0

Please sign in to comment.