Skip to content

Commit

Permalink
The Attributes iterator now gets the values.
Browse files Browse the repository at this point in the history
  • Loading branch information
fpagliughi committed Jan 14, 2022
1 parent e22fb0f commit 04c53da
Showing 1 changed file with 33 additions and 4 deletions.
37 changes: 33 additions & 4 deletions src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::str::FromStr;

use libc::{c_char, dev_t};

use list::EntryList;
use list::{EntryList, Entry};
use Udev;
use {ffi, util};

Expand Down Expand Up @@ -63,7 +63,10 @@ as_ffi_with_context!(Device, device, ffi::udev_device, ffi::udev_device_ref);
pub type Properties<'a> = EntryList<'a, Device>;

/// A convenience alias for a list of attributes, bound to a device.
pub type Attributes<'a> = EntryList<'a, Device>;
pub struct Attributes<'a> {
entries: EntryList<'a, Device>,
device: &'a Device,
}

impl Device {
/// Creates a device for a given syspath.
Expand Down Expand Up @@ -337,8 +340,11 @@ impl Device {
/// ```
pub fn attributes(&self) -> Attributes {
Attributes {
entry: unsafe { ffi::udev_device_get_sysattr_list_entry(self.device) },
phantom: PhantomData,
entries: EntryList {
entry: unsafe { ffi::udev_device_get_sysattr_list_entry(self.device) },
phantom: PhantomData,
},
device: self,
}
}

Expand All @@ -347,3 +353,26 @@ impl Device {
unsafe { util::ptr_to_os_str(ffi::udev_device_get_action(self.device)) }
}
}

impl<'a> Iterator for Attributes<'a> {
type Item = Entry<'a>;

// The list of sysattr entries only contains the attribute names, with
// the values being empty. To get the value, each has to be queried.
fn next(&mut self) -> Option<Entry<'a>> {
match self.entries.next() {
Some(Entry { name, value: _ }) => {
Some( Entry {
name,
value: self.device.attribute_value(name),
})
},
None => None,
}
}

fn size_hint(&self) -> (usize, Option<usize>) {
(0, None)
}
}

0 comments on commit 04c53da

Please sign in to comment.