Skip to content

Commit

Permalink
mio-udev: update udev to 0.4
Browse files Browse the repository at this point in the history
Signed-off-by: Jean Pierre Dudey <jeandudey@hotmail.com>
  • Loading branch information
jeandudey committed Jun 20, 2020
1 parent a395fa6 commit 96fdb1f
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 43 deletions.
4 changes: 2 additions & 2 deletions mio-udev/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "mio-udev"
version = "0.2.0"
version = "0.3.0"
authors = ["Jean Pierre Dudey <jeandudey@hotmail.com>"]
license = "Apache-2.0/MIT"
description = """
Expand All @@ -12,6 +12,6 @@ documentation = "https://docs.rs/mio-udev"
edition = "2018"

[dependencies]
udev = "0.2"
udev = "0.4"
mio = "0.6"
libc = "0.2"
37 changes: 21 additions & 16 deletions mio-udev/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
#![cfg(target_os = "linux")]

pub use udev::{
Attribute, Attributes, Context, Device, Enumerator, Error as UdevError,
Event, EventType, Properties, Property,
Attribute, Attributes, Device, Enumerator, Event, EventType, Properties,
Property,
};

mod util;
Expand All @@ -58,58 +58,63 @@ pub struct MonitorBuilder {
impl MonitorBuilder {
/// Creates a new `MonitorSocket`.
#[inline(always)]
pub fn new(context: &Context) -> io::Result<Self> {
pub fn new() -> io::Result<Self> {
Ok(MonitorBuilder {
builder: udev::MonitorBuilder::new(context)?,
builder: udev::MonitorBuilder::new()?,
})
}

#[inline(always)]
fn map(builder: udev::MonitorBuilder) -> Self {
MonitorBuilder { builder }
}

/// 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<()>
pub fn match_subsystem<T>(self, subsystem: T) -> io::Result<Self>
where
T: AsRef<OsStr>,
{
Ok(self.builder.match_subsystem::<T>(subsystem)?)
self.builder.match_subsystem::<T>(subsystem).map(Self::map)
}

/// 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,
self,
subsystem: T,
devtype: U,
) -> io::Result<()>
) -> io::Result<Self>
where
T: AsRef<OsStr>,
U: AsRef<OsStr>,
{
Ok(self
.builder
.match_subsystem_devtype::<T, U>(subsystem, devtype)?)
self.builder
.match_subsystem_devtype::<T, U>(subsystem, devtype)
.map(Self::map)
}

/// 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<()>
pub fn match_tag<T>(self, tag: T) -> io::Result<Self>
where
T: AsRef<OsStr>,
{
Ok(self.builder.match_tag::<T>(tag)?)
self.builder.match_tag::<T>(tag).map(Self::map)
}

/// Removes all filters currently set on the monitor.
#[inline(always)]
pub fn clear_filters(&mut self) -> io::Result<()> {
Ok(self.builder.clear_filters()?)
pub fn clear_filters(self) -> io::Result<Self> {
self.builder.clear_filters().map(Self::map)
}

/// Listens for events matching the current filters.
///
/// This method consumes the `MonitorBuilder`.
pub fn listen(self) -> io::Result<MonitorSocket> {
Ok(MonitorSocket::new(self.builder.listen()?)?)
MonitorSocket::new(self.builder.listen()?)
}
}

Expand Down
6 changes: 2 additions & 4 deletions tokio-udev/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "tokio-udev"
version = "0.2.0"
version = "0.3.0"
authors = ["Jean Pierre Dudey <jeandudey@hotmail.com>"]
license = "Apache-2.0/MIT"
description = """
Expand All @@ -12,10 +12,8 @@ documentation = "https://docs.rs/tokio-udev"
edition = "2018"

[dependencies]
libc = "0.2"

mio = "0.6"
mio-udev = { path = "../mio-udev", version = "0.2.0" }
mio-udev = { path = "../mio-udev", version = "0.3.0" }

futures-core = "0.3"
tokio = { version = "0.2", features = ["io-driver"] }
Expand Down
11 changes: 5 additions & 6 deletions tokio-udev/examples/usb_hotplug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,16 @@

use futures_util::future::ready;
use futures_util::stream::StreamExt;
use tokio_udev::{Context, MonitorBuilder};
use tokio_udev::MonitorBuilder;

#[tokio::main]
async fn main() {
let context = Context::new().unwrap();
let mut builder = MonitorBuilder::new(&context).unwrap();
builder
let builder = MonitorBuilder::new()
.expect("Couldn't create builder")
.match_subsystem_devtype("usb", "usb_device")
.unwrap();
.expect("Failed to add filter for USB devices");

let monitor = builder.listen().unwrap();
let monitor = builder.listen().expect("Couldn't create MonitorSocket");
monitor
.for_each(|event| {
println!(
Expand Down
34 changes: 19 additions & 15 deletions tokio-udev/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
#![cfg(target_os = "linux")]

pub use mio_udev::{
Attribute, Attributes, Context, Device, Enumerator, Event, EventType,
Properties, Property, UdevError,
Attribute, Attributes, Device, Enumerator, Event, EventType, Properties,
Property,
};

use std::ffi::OsStr;
Expand All @@ -57,51 +57,55 @@ pub struct MonitorBuilder {
impl MonitorBuilder {
/// Creates a new `MonitorSocket`.
#[inline(always)]
pub fn new(context: &mio_udev::Context) -> io::Result<Self> {
pub fn new() -> io::Result<Self> {
Ok(MonitorBuilder {
builder: mio_udev::MonitorBuilder::new(context)?,
builder: mio_udev::MonitorBuilder::new()?,
})
}

fn map(builder: mio_udev::MonitorBuilder) -> Self {
MonitorBuilder { builder }
}

/// 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<()>
pub fn match_subsystem<T>(self, subsystem: T) -> io::Result<Self>
where
T: AsRef<OsStr>,
{
Ok(self.builder.match_subsystem::<T>(subsystem)?)
self.builder.match_subsystem::<T>(subsystem).map(Self::map)
}

/// 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,
self,
subsystem: T,
devtype: U,
) -> io::Result<()>
) -> io::Result<Self>
where
T: AsRef<OsStr>,
U: AsRef<OsStr>,
{
Ok(self
.builder
.match_subsystem_devtype::<T, U>(subsystem, devtype)?)
self.builder
.match_subsystem_devtype::<T, U>(subsystem, devtype)
.map(Self::map)
}

/// 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<()>
pub fn match_tag<T>(self, tag: T) -> io::Result<Self>
where
T: AsRef<OsStr>,
{
Ok(self.builder.match_tag::<T>(tag)?)
self.builder.match_tag::<T>(tag).map(Self::map)
}

/// Removes all filters currently set on the monitor.
#[inline(always)]
pub fn clear_filters(&mut self) -> io::Result<()> {
Ok(self.builder.clear_filters()?)
pub fn clear_filters(self) -> io::Result<Self> {
self.builder.clear_filters().map(Self::map)
}

/// Listens for events matching the current filters.
Expand Down

0 comments on commit 96fdb1f

Please sign in to comment.