Skip to content

Commit

Permalink
Merge pull request #1101 from GuillaumeGomez/into-iter
Browse files Browse the repository at this point in the history
Implement `IntoIterator` for `Disks`, `Components` and `Users` types
  • Loading branch information
GuillaumeGomez authored Oct 15, 2023
2 parents 96be47a + c305d24 commit 0eca9ce
Showing 1 changed file with 75 additions and 21 deletions.
96 changes: 75 additions & 21 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2023,6 +2023,24 @@ impl Default for Disks {
}
}

impl<'a> IntoIterator for &'a Disks {
type Item = &'a Disk;
type IntoIter = std::slice::Iter<'a, Disk>;

fn into_iter(self) -> Self::IntoIter {
self.disks().iter()
}
}

impl<'a> IntoIterator for &'a mut Disks {
type Item = &'a mut Disk;
type IntoIter = std::slice::IterMut<'a, Disk>;

fn into_iter(self) -> Self::IntoIter {
self.disks_mut().iter_mut()
}
}

impl Disks {
/// Creates a new [`Disks`][crate::Disks] type.
///
Expand Down Expand Up @@ -2181,20 +2199,6 @@ pub enum DiskKind {
Unknown(isize),
}

impl std::ops::Deref for Components {
type Target = [Component];

fn deref(&self) -> &Self::Target {
self.components()
}
}

impl std::ops::DerefMut for Components {
fn deref_mut(&mut self) -> &mut Self::Target {
self.components_mut()
}
}

/// Interacting with users.
///
/// ```no_run
Expand Down Expand Up @@ -2229,6 +2233,24 @@ impl std::ops::DerefMut for Users {
}
}

impl<'a> IntoIterator for &'a Users {
type Item = &'a User;
type IntoIter = std::slice::Iter<'a, User>;

fn into_iter(self) -> Self::IntoIter {
self.users().iter()
}
}

impl<'a> IntoIterator for &'a mut Users {
type Item = &'a mut User;
type IntoIter = std::slice::IterMut<'a, User>;

fn into_iter(self) -> Self::IntoIter {
self.users_mut().iter_mut()
}
}

impl Users {
/// Creates a new [`Components`][crate::Components] type.
///
Expand Down Expand Up @@ -2988,7 +3010,7 @@ impl fmt::Display for MacAddr {
///
/// let mut components = Components::new();
/// components.refresh_list();
/// for component in components.iter() {
/// for component in &components {
/// eprintln!("{component:?}");
/// }
/// ```
Expand All @@ -3002,6 +3024,38 @@ impl Default for Components {
}
}

impl std::ops::Deref for Components {
type Target = [Component];

fn deref(&self) -> &Self::Target {
self.components()
}
}

impl std::ops::DerefMut for Components {
fn deref_mut(&mut self) -> &mut Self::Target {
self.components_mut()
}
}

impl<'a> IntoIterator for &'a Components {
type Item = &'a Component;
type IntoIter = std::slice::Iter<'a, Component>;

fn into_iter(self) -> Self::IntoIter {
self.components().iter()
}
}

impl<'a> IntoIterator for &'a mut Components {
type Item = &'a mut Component;
type IntoIter = std::slice::IterMut<'a, Component>;

fn into_iter(self) -> Self::IntoIter {
self.components_mut().iter_mut()
}
}

impl Components {
/// Creates a new [`Components`][crate::Components] type.
///
Expand All @@ -3010,7 +3064,7 @@ impl Components {
///
/// let mut components = Components::new();
/// components.refresh_list();
/// for component in components.iter() {
/// for component in &components {
/// eprintln!("{component:?}");
/// }
/// ```
Expand Down Expand Up @@ -3120,7 +3174,7 @@ impl Components {
///
/// let mut components = Components::new();
/// components.refresh_list();
/// for component in components.iter() {
/// for component in &components {
/// println!("{}°C", component.temperature());
/// }
/// ```
Expand All @@ -3140,7 +3194,7 @@ impl Component {
///
/// let mut components = Components::new();
/// components.refresh_list();
/// for component in components.iter() {
/// for component in &components {
/// println!("{}°C", component.temperature());
/// }
/// ```
Expand All @@ -3158,7 +3212,7 @@ impl Component {
///
/// let mut components = Components::new();
/// components.refresh_list();
/// for component in components.iter() {
/// for component in &components {
/// println!("{}°C", component.max());
/// }
/// ```
Expand All @@ -3178,7 +3232,7 @@ impl Component {
///
/// let mut components = Components::new();
/// components.refresh_list();
/// for component in components.iter() {
/// for component in &components {
/// println!("{:?}°C", component.critical());
/// }
/// ```
Expand All @@ -3197,7 +3251,7 @@ impl Component {
///
/// let mut components = Components::new();
/// components.refresh_list();
/// for component in components.iter() {
/// for component in &components {
/// println!("{}", component.label());
/// }
/// ```
Expand Down

0 comments on commit 0eca9ce

Please sign in to comment.