Skip to content

Commit

Permalink
Merge pull request #4 from jeandudey/2020_06_20-rustfmt
Browse files Browse the repository at this point in the history
{mio,tokio}-udev: format code with rustfmt
  • Loading branch information
jeandudey authored Jun 20, 2020
2 parents 35fd0d1 + 681626e commit cd02d3e
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 48 deletions.
65 changes: 43 additions & 22 deletions mio-udev/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,20 @@
//! use mio_udev;
//! ```
pub use udev::{Attribute, Attributes, Context, Device, Enumerator, Event,
EventType, Property, Properties, Error as UdevError};
pub use udev::{
Attribute, Attributes, Context, Device, Enumerator, Error as UdevError,
Event, EventType, Properties, Property,
};

mod util;

use std::ffi::OsStr;
use std::io;
use std::os::unix::io::{AsRawFd, RawFd};
use std::ffi::OsStr;

use mio::{Ready, Poll, PollOpt, Token};
use mio::event::Evented;
use mio::unix::EventedFd;
use mio::{Poll, PollOpt, Ready, Token};

/// Monitors for device events.
///
Expand All @@ -47,33 +49,42 @@ impl MonitorBuilder {
/// Creates a new `MonitorSocket`.
#[inline(always)]
pub fn new(context: &Context) -> io::Result<Self> {
Ok(MonitorBuilder { builder: udev::MonitorBuilder::new(context)? })
Ok(MonitorBuilder {
builder: udev::MonitorBuilder::new(context)?,
})
}

/// Adds a filter that matches events for devices with the given subsystem.
#[inline(always)]
pub fn match_subsystem<T>(&mut self, subsystem: T) -> io::Result<()>
where T: AsRef<OsStr>,
where
T: AsRef<OsStr>,
{
Ok(self.builder.match_subsystem::<T>(subsystem)?)
}

/// Adds a filter that matches events for devices with the given subsystem
/// and device type.
#[inline(always)]
pub fn match_subsystem_devtype<T, U>(&mut self,
subsystem: T,
devtype: U) -> io::Result<()>
where T: AsRef<OsStr>,
U: AsRef<OsStr>,
pub fn match_subsystem_devtype<T, U>(
&mut self,
subsystem: T,
devtype: U,
) -> io::Result<()>
where
T: AsRef<OsStr>,
U: AsRef<OsStr>,
{
Ok(self.builder.match_subsystem_devtype::<T, U>(subsystem, devtype)?)
Ok(self
.builder
.match_subsystem_devtype::<T, U>(subsystem, devtype)?)
}

/// Adds a filter that matches events for devices with the given tag.
#[inline(always)]
pub fn match_tag<T>(&mut self, tag: T) -> io::Result<()>
where T: AsRef<OsStr>,
where
T: AsRef<OsStr>,
{
Ok(self.builder.match_tag::<T>(tag)?)
}
Expand All @@ -100,23 +111,25 @@ pub struct MonitorSocket {

impl MonitorSocket {
fn new(monitor: udev::MonitorSocket) -> io::Result<MonitorSocket> {
use libc::{fcntl, F_GETFD, FD_CLOEXEC, F_SETFD, F_GETFL, F_SETFL, O_NONBLOCK};
use crate::util::cvt;
use libc::{
fcntl, FD_CLOEXEC, F_GETFD, F_GETFL, F_SETFD, F_SETFL, O_NONBLOCK,
};

let fd = monitor.as_raw_fd();

// Make sure the udev file descriptor is marked as CLOEXEC.
let r = unsafe { cvt(fcntl(fd, F_GETFD))? };

if !((r & FD_CLOEXEC) == FD_CLOEXEC) {
if (r & FD_CLOEXEC) != FD_CLOEXEC {
unsafe { cvt(fcntl(fd, F_SETFD, r | FD_CLOEXEC))? };
}

// Some older versions of udev are not non-blocking by default,
// so make sure this is set
let r = unsafe { cvt(fcntl(fd, F_GETFL))? };

if !((r & O_NONBLOCK) == O_NONBLOCK) {
if (r & O_NONBLOCK) != O_NONBLOCK {
unsafe { cvt(fcntl(fd, F_SETFL, r | O_NONBLOCK))? };
}

Expand All @@ -130,15 +143,23 @@ impl MonitorSocket {
}

impl Evented for MonitorSocket {
fn register(&self, poll: &Poll, token: Token, interest: Ready, opts: PollOpt)
-> io::Result<()>
{
fn register(
&self,
poll: &Poll,
token: Token,
interest: Ready,
opts: PollOpt,
) -> io::Result<()> {
EventedFd(&self.fd()).register(poll, token, interest, opts)
}

fn reregister(&self, poll: &Poll, token: Token, interest: Ready, opts: PollOpt)
-> io::Result<()>
{
fn reregister(
&self,
poll: &Poll,
token: Token,
interest: Ready,
opts: PollOpt,
) -> io::Result<()> {
EventedFd(&self.fd()).reregister(poll, token, interest, opts)
}

Expand Down
2 changes: 1 addition & 1 deletion mio-udev/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ macro_rules! one {

one! { i8 i16 i32 i64 isize u8 u16 u32 u64 usize }

pub fn cvt<T: One + PartialEq + Neg<Output=T>>(t: T) -> io::Result<T> {
pub fn cvt<T: One + PartialEq + Neg<Output = T>>(t: T) -> io::Result<T> {
let one: T = T::one();
if t == -one {
Err(io::Error::last_os_error())
Expand Down
1 change: 1 addition & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
max_width = 80
18 changes: 13 additions & 5 deletions tokio-udev/examples/usb_hotplug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,19 @@ use tokio_udev::{Context, MonitorBuilder};
async fn main() {
let context = Context::new().unwrap();
let mut builder = MonitorBuilder::new(&context).unwrap();
builder.match_subsystem_devtype("usb", "usb_device").unwrap();
builder
.match_subsystem_devtype("usb", "usb_device")
.unwrap();

let monitor = builder.listen().unwrap();
monitor.for_each(|event| {
println!("Hotplug event: {}: {}", event.event_type(), event.device().syspath().display());
ready(())
}).await
monitor
.for_each(|event| {
println!(
"Hotplug event: {}: {}",
event.event_type(),
event.device().syspath().display()
);
ready(())
})
.await
}
65 changes: 45 additions & 20 deletions tokio-udev/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,19 @@
//! use tokio_udev;
//! ```
pub use mio_udev::{Attribute, Attributes, Context, Device, Enumerator, Event,
EventType, Property, Properties, UdevError};
pub use mio_udev::{
Attribute, Attributes, Context, Device, Enumerator, Event, EventType,
Properties, Property, UdevError,
};

use std::io;
use std::ffi::OsStr;
use std::io;
use std::pin::Pin;
use std::sync::Mutex;
use std::task::Poll;

use tokio::io::PollEvented;
use futures_core::stream::Stream;
use tokio::io::PollEvented;

/// Monitors for device events.
///
Expand All @@ -46,33 +48,42 @@ impl MonitorBuilder {
/// Creates a new `MonitorSocket`.
#[inline(always)]
pub fn new(context: &mio_udev::Context) -> io::Result<Self> {
Ok(MonitorBuilder { builder: mio_udev::MonitorBuilder::new(context)? })
Ok(MonitorBuilder {
builder: mio_udev::MonitorBuilder::new(context)?,
})
}

/// Adds a filter that matches events for devices with the given subsystem.
#[inline(always)]
pub fn match_subsystem<T>(&mut self, subsystem: T) -> io::Result<()>
where T: AsRef<OsStr>,
where
T: AsRef<OsStr>,
{
Ok(self.builder.match_subsystem::<T>(subsystem)?)
}

/// Adds a filter that matches events for devices with the given subsystem
/// and device type.
#[inline(always)]
pub fn match_subsystem_devtype<T, U>(&mut self,
subsystem: T,
devtype: U) -> io::Result<()>
where T: AsRef<OsStr>,
U: AsRef<OsStr>,
pub fn match_subsystem_devtype<T, U>(
&mut self,
subsystem: T,
devtype: U,
) -> io::Result<()>
where
T: AsRef<OsStr>,
U: AsRef<OsStr>,
{
Ok(self.builder.match_subsystem_devtype::<T, U>(subsystem, devtype)?)
Ok(self
.builder
.match_subsystem_devtype::<T, U>(subsystem, devtype)?)
}

/// Adds a filter that matches events for devices with the given tag.
#[inline(always)]
pub fn match_tag<T>(&mut self, tag: T) -> io::Result<()>
where T: AsRef<OsStr>,
where
T: AsRef<OsStr>,
{
Ok(self.builder.match_tag::<T>(tag)?)
}
Expand All @@ -98,7 +109,9 @@ pub struct MonitorSocket {

impl MonitorSocket {
fn new(monitor: mio_udev::MonitorSocket) -> io::Result<MonitorSocket> {
Ok(MonitorSocket { inner: Mutex::new(Inner::new(monitor)?), })
Ok(MonitorSocket {
inner: Mutex::new(Inner::new(monitor)?),
})
}
}

Expand All @@ -108,7 +121,10 @@ unsafe impl Sync for MonitorSocket {}
impl Stream for MonitorSocket {
type Item = mio_udev::Event;

fn poll_next(self: Pin<&mut Self>, cx: &mut std::task::Context) -> Poll<Option<Self::Item>> {
fn poll_next(
self: Pin<&mut Self>,
cx: &mut std::task::Context,
) -> Poll<Option<Self::Item>> {
self.inner.lock().unwrap().poll_receive(cx)
}
}
Expand All @@ -119,20 +135,29 @@ struct Inner {

impl Inner {
fn new(monitor: mio_udev::MonitorSocket) -> io::Result<Inner> {
Ok(Inner { io: PollEvented::new(monitor)?, })
Ok(Inner {
io: PollEvented::new(monitor)?,
})
}

fn poll_receive(&mut self, cx: &mut std::task::Context) -> Poll<Option<mio_udev::Event>> {
if let Poll::Pending = self.io.poll_read_ready(cx, mio::Ready::readable()) {
fn poll_receive(
&mut self,
cx: &mut std::task::Context,
) -> Poll<Option<mio_udev::Event>> {
if let Poll::Pending =
self.io.poll_read_ready(cx, mio::Ready::readable())
{
return Poll::Pending;
}

match self.io.get_mut().next() {
Some(event) => Poll::Ready(Some(event)),
None => {
self.io.clear_read_ready(cx, mio::Ready::readable()).unwrap();
self.io
.clear_read_ready(cx, mio::Ready::readable())
.unwrap();
Poll::Pending
},
}
}
}
}

0 comments on commit cd02d3e

Please sign in to comment.