Skip to content

Commit

Permalink
Merge pull request #37 from TomzBench/fix-socket-iter
Browse files Browse the repository at this point in the history
Export SocketIter and remove SocketIter cloneing
  • Loading branch information
Drakulix authored Sep 22, 2023
2 parents 978e24f + 00ca2de commit aa44477
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 20 deletions.
5 changes: 4 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ pub use enumerator::{Devices, Enumerator};
#[cfg(feature = "hwdb")]
pub use hwdb::Hwdb;
pub use list::{Entry, List};
pub use monitor::{Builder as MonitorBuilder, Event, EventType, Socket as MonitorSocket};
pub use monitor::{
Builder as MonitorBuilder, Event, EventType, Socket as MonitorSocket,
SocketIter as MonitorSocketIter,
};
pub use udev::Udev;

macro_rules! try_alloc {
Expand Down
29 changes: 10 additions & 19 deletions src/monitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ pub struct Socket {
impl Socket {
/// Create an iterator of socket event messages
pub fn iter(&self) -> SocketIter {
SocketIter::new(self)
SocketIter::new(&self)

Check warning on line 159 in src/monitor.rs

View workflow job for this annotation

GitHub Actions / clippy

this expression creates a reference which is immediately dereferenced by the compiler

warning: this expression creates a reference which is immediately dereferenced by the compiler --> src/monitor.rs:159:25 | 159 | SocketIter::new(&self) | ^^^^^ help: change this to: `self` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow = note: `#[warn(clippy::needless_borrow)]` on by default
}
}

Expand Down Expand Up @@ -186,37 +186,28 @@ impl AsFd for Socket {
}
}

pub struct SocketIter {
udev: Udev,
monitor: *mut ffi::udev_monitor,
/// Iterator of socket events
pub struct SocketIter<'a> {
socket: &'a Socket,
}

impl SocketIter {
impl<'a> SocketIter<'a> {
/// Create a socket by cloning the underlying udev instance
fn new(socket: &Socket) -> SocketIter {
SocketIter {
udev: socket.inner.udev.clone(),
monitor: unsafe { ffi::udev_monitor_ref(socket.inner.monitor) },
}
}
}

impl Drop for SocketIter {
fn drop(&mut self) {
unsafe { ffi::udev_monitor_unref(self.monitor) };
fn new(socket: &'a Socket) -> SocketIter<'a> {
SocketIter { socket }
}
}

impl Iterator for SocketIter {
impl<'a> Iterator for SocketIter<'a> {
type Item = Event;

fn next(&mut self) -> Option<Event> {
let ptr = unsafe { ffi::udev_monitor_receive_device(self.monitor) };
let ptr = unsafe { ffi::udev_monitor_receive_device(self.socket.inner.monitor) };

if ptr.is_null() {
None
} else {
let device = Device::from_raw(self.udev.clone(), ptr);
let device = Device::from_raw(self.socket.inner.udev.clone(), ptr);
Some(Event { device })
}
}
Expand Down

0 comments on commit aa44477

Please sign in to comment.