Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement IntoIterator for Disks, Components and Users types #1101

Merged
merged 1 commit into from
Oct 15, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 75 additions & 21 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2021,6 +2021,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 @@ -2179,20 +2197,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 @@ -2227,6 +2231,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 @@ -2986,7 +3008,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 @@ -3000,6 +3022,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 @@ -3008,7 +3062,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 @@ -3118,7 +3172,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 @@ -3138,7 +3192,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 @@ -3156,7 +3210,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 @@ -3176,7 +3230,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 @@ -3195,7 +3249,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
Loading