diff --git a/mio-udev/src/lib.rs b/mio-udev/src/lib.rs index 49cf110..36b414b 100644 --- a/mio-udev/src/lib.rs +++ b/mio-udev/src/lib.rs @@ -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. /// @@ -47,13 +49,16 @@ impl MonitorBuilder { /// Creates a new `MonitorSocket`. #[inline(always)] pub fn new(context: &Context) -> io::Result { - 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(&mut self, subsystem: T) -> io::Result<()> - where T: AsRef, + where + T: AsRef, { Ok(self.builder.match_subsystem::(subsystem)?) } @@ -61,19 +66,25 @@ impl MonitorBuilder { /// Adds a filter that matches events for devices with the given subsystem /// and device type. #[inline(always)] - pub fn match_subsystem_devtype(&mut self, - subsystem: T, - devtype: U) -> io::Result<()> - where T: AsRef, - U: AsRef, + pub fn match_subsystem_devtype( + &mut self, + subsystem: T, + devtype: U, + ) -> io::Result<()> + where + T: AsRef, + U: AsRef, { - Ok(self.builder.match_subsystem_devtype::(subsystem, devtype)?) + Ok(self + .builder + .match_subsystem_devtype::(subsystem, devtype)?) } /// Adds a filter that matches events for devices with the given tag. #[inline(always)] pub fn match_tag(&mut self, tag: T) -> io::Result<()> - where T: AsRef, + where + T: AsRef, { Ok(self.builder.match_tag::(tag)?) } @@ -100,15 +111,17 @@ pub struct MonitorSocket { impl MonitorSocket { fn new(monitor: udev::MonitorSocket) -> io::Result { - 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))? }; } @@ -116,7 +129,7 @@ impl MonitorSocket { // 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))? }; } @@ -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) } diff --git a/mio-udev/src/util.rs b/mio-udev/src/util.rs index 47fb1d1..c481c1d 100644 --- a/mio-udev/src/util.rs +++ b/mio-udev/src/util.rs @@ -13,7 +13,7 @@ macro_rules! one { one! { i8 i16 i32 i64 isize u8 u16 u32 u64 usize } -pub fn cvt>(t: T) -> io::Result { +pub fn cvt>(t: T) -> io::Result { let one: T = T::one(); if t == -one { Err(io::Error::last_os_error()) diff --git a/rustfmt.toml b/rustfmt.toml new file mode 100644 index 0000000..df99c69 --- /dev/null +++ b/rustfmt.toml @@ -0,0 +1 @@ +max_width = 80 diff --git a/tokio-udev/examples/usb_hotplug.rs b/tokio-udev/examples/usb_hotplug.rs index 585446e..e155958 100644 --- a/tokio-udev/examples/usb_hotplug.rs +++ b/tokio-udev/examples/usb_hotplug.rs @@ -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 } diff --git a/tokio-udev/src/lib.rs b/tokio-udev/src/lib.rs index f13b68d..5985729 100644 --- a/tokio-udev/src/lib.rs +++ b/tokio-udev/src/lib.rs @@ -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. /// @@ -46,13 +48,16 @@ impl MonitorBuilder { /// Creates a new `MonitorSocket`. #[inline(always)] pub fn new(context: &mio_udev::Context) -> io::Result { - 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(&mut self, subsystem: T) -> io::Result<()> - where T: AsRef, + where + T: AsRef, { Ok(self.builder.match_subsystem::(subsystem)?) } @@ -60,19 +65,25 @@ impl MonitorBuilder { /// Adds a filter that matches events for devices with the given subsystem /// and device type. #[inline(always)] - pub fn match_subsystem_devtype(&mut self, - subsystem: T, - devtype: U) -> io::Result<()> - where T: AsRef, - U: AsRef, + pub fn match_subsystem_devtype( + &mut self, + subsystem: T, + devtype: U, + ) -> io::Result<()> + where + T: AsRef, + U: AsRef, { - Ok(self.builder.match_subsystem_devtype::(subsystem, devtype)?) + Ok(self + .builder + .match_subsystem_devtype::(subsystem, devtype)?) } /// Adds a filter that matches events for devices with the given tag. #[inline(always)] pub fn match_tag(&mut self, tag: T) -> io::Result<()> - where T: AsRef, + where + T: AsRef, { Ok(self.builder.match_tag::(tag)?) } @@ -98,7 +109,9 @@ pub struct MonitorSocket { impl MonitorSocket { fn new(monitor: mio_udev::MonitorSocket) -> io::Result { - Ok(MonitorSocket { inner: Mutex::new(Inner::new(monitor)?), }) + Ok(MonitorSocket { + inner: Mutex::new(Inner::new(monitor)?), + }) } } @@ -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> { + fn poll_next( + self: Pin<&mut Self>, + cx: &mut std::task::Context, + ) -> Poll> { self.inner.lock().unwrap().poll_receive(cx) } } @@ -119,20 +135,29 @@ struct Inner { impl Inner { fn new(monitor: mio_udev::MonitorSocket) -> io::Result { - Ok(Inner { io: PollEvented::new(monitor)?, }) + Ok(Inner { + io: PollEvented::new(monitor)?, + }) } - fn poll_receive(&mut self, cx: &mut std::task::Context) -> Poll> { - if let Poll::Pending = self.io.poll_read_ready(cx, mio::Ready::readable()) { + fn poll_receive( + &mut self, + cx: &mut std::task::Context, + ) -> Poll> { + 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 - }, + } } } }