Rust 2018 edition changes

This commit is contained in:
Travis Burtrum 2019-09-29 23:49:23 -04:00
parent 95cfd637b9
commit f4083343da
5 changed files with 15 additions and 29 deletions

View File

@ -9,6 +9,8 @@ description = "Linux keyboard mapper"
repository = "https://code.moparisthebest.com/moparisthebest/rusty-keys" repository = "https://code.moparisthebest.com/moparisthebest/rusty-keys"
keywords = ["linux", "input", "keyboard", "keymapper"] keywords = ["linux", "input", "keyboard", "keymapper"]
edition = "2018"
include = [ include = [
"src/**", "src/**",
"Cargo.toml", "Cargo.toml",

View File

@ -5,7 +5,7 @@ use libc::c_int;
use nix::{self, fcntl, unistd, errno::Errno}; use nix::{self, fcntl, unistd, errno::Errno};
use nix::sys::stat; use nix::sys::stat;
use ffi::*; use ffi::*;
use {Result as Res, Device}; use crate::{Result as Res, Device};
use std::collections::hash_map::Values; use std::collections::hash_map::Values;
#[cfg(feature = "udev")] #[cfg(feature = "udev")]
@ -22,28 +22,12 @@ impl Builder {
/// Create a builder from the specified path. /// Create a builder from the specified path.
pub fn open<P: AsRef<Path>>(path: P) -> Res<Self> { pub fn open<P: AsRef<Path>>(path: P) -> Res<Self> {
Ok(Builder { Ok(Builder {
fd: try!(fcntl::open(path.as_ref(), fcntl::OFlag::O_WRONLY | fcntl::OFlag::O_NONBLOCK, stat::Mode::empty())), fd: fcntl::open(path.as_ref(), fcntl::OFlag::O_WRONLY | fcntl::OFlag::O_NONBLOCK, stat::Mode::empty())?,
def: unsafe { mem::zeroed() }, def: unsafe { mem::zeroed() },
abs: None, abs: None,
}) })
} }
#[cfg(feature = "udev")]
/// Create a builder from the default path taken from udev.
pub fn default() -> Res<Self> {
let context = try!(udev::Context::new());
let mut enumerator = try!(udev::Enumerator::new(&context));
try!(enumerator.match_subsystem("misc"));
try!(enumerator.match_sysname("uinput"));
let device = try!(try!(enumerator.scan_devices())
.next().ok_or(Error::NotFound));
Builder::open(try!(device.devnode().ok_or(Error::NotFound)))
}
#[cfg(not(feature = "udev"))]
/// Create a builder from `/dev/uinput`. /// Create a builder from `/dev/uinput`.
pub fn default() -> Res<Self> { pub fn default() -> Res<Self> {
Builder::open("/dev/uinput") Builder::open("/dev/uinput")
@ -51,11 +35,11 @@ impl Builder {
/// Set the name. /// Set the name.
pub fn name<T: AsRef<str>>(mut self, value: T) -> Res<Self> { pub fn name<T: AsRef<str>>(mut self, value: T) -> Res<Self> {
let string = try!(CString::new(value.as_ref())); let string = CString::new(value.as_ref())?;
let bytes = string.as_bytes_with_nul(); let bytes = string.as_bytes_with_nul();
if bytes.len() > UINPUT_MAX_NAME_SIZE as usize { if bytes.len() > UINPUT_MAX_NAME_SIZE as usize {
try!(Err(nix::Error::from_errno(Errno::EINVAL))); Err(nix::Error::from_errno(Errno::EINVAL))?;
} }
(&mut self.def.name)[..bytes.len()] (&mut self.def.name)[..bytes.len()]
@ -276,10 +260,10 @@ impl Builder {
let ptr = &self.def as *const _ as *const u8; let ptr = &self.def as *const _ as *const u8;
let size = mem::size_of_val(&self.def); let size = mem::size_of_val(&self.def);
try!(unistd::write(self.fd, slice::from_raw_parts(ptr, size))); unistd::write(self.fd, slice::from_raw_parts(ptr, size))?;
//todo: try!(Errno::result(ui_dev_create(self.fd))); //todo: try!(Errno::result(ui_dev_create(self.fd)));
// try1: Errno::result(ui_dev_create(self.fd)).unwrap(); // try1: Errno::result(ui_dev_create(self.fd)).unwrap();
try!(Errno::result(ui_dev_create(self.fd))); Errno::result(ui_dev_create(self.fd))?;
} }
Ok(Device::new(self.fd)) Ok(Device::new(self.fd))

View File

@ -2,7 +2,7 @@ use std::{mem, ptr, slice};
use libc::{timeval, gettimeofday, input_event, c_int}; use libc::{timeval, gettimeofday, input_event, c_int};
use nix::{unistd, errno::Errno}; use nix::{unistd, errno::Errno};
use ffi::*; use ffi::*;
use {Result as Res}; use crate::{Result as Res};
/// The virtual device. /// The virtual device.
pub struct Device { pub struct Device {
@ -37,7 +37,7 @@ impl Device {
let ptr = event as *const _ as *const u8; let ptr = event as *const _ as *const u8;
let size = mem::size_of_val(event); let size = mem::size_of_val(event);
try!(unistd::write(self.fd, slice::from_raw_parts(ptr, size))); unistd::write(self.fd, slice::from_raw_parts(ptr, size))?;
} }
Ok(()) Ok(())
@ -65,8 +65,8 @@ impl Device {
/// Send a press and release event. /// Send a press and release event.
pub fn click(&self, kind: c_int, code: c_int) -> Res<()> { pub fn click(&self, kind: c_int, code: c_int) -> Res<()> {
try!(self.press(kind, code)); self.press(kind, code)?;
try!(self.release(kind, code)); self.release(kind, code)?;
Ok(()) Ok(())
} }

View File

@ -3,7 +3,7 @@ use std::fs::File;
use std::io::Read; use std::io::Read;
use std::os::unix::io::AsRawFd; use std::os::unix::io::AsRawFd;
use libc::{input_event, c_int}; use libc::{input_event, c_int};
use {Error,Result}; use crate::{Error,Result};
ioctl_write_ptr!(eviocgrab, b'E', 0x90, c_int); ioctl_write_ptr!(eviocgrab, b'E', 0x90, c_int);

View File

@ -1,5 +1,5 @@
use device::Device; use crate::Device;
use ffi::*; use ffi::*;
use libc::{c_int, input_event}; use libc::{c_int, input_event};
@ -8,7 +8,7 @@ use std::fs::File;
use std::io::Read; use std::io::Read;
use std::collections::HashMap; use std::collections::HashMap;
use {Error, Result}; use crate::{Error, Result};
// 1 is down, 0 is up // 1 is down, 0 is up
const DOWN: i32 = 1; const DOWN: i32 = 1;