diff --git a/src/common.rs b/src/common.rs index 5d0cfeb60..f812d33a3 100644 --- a/src/common.rs +++ b/src/common.rs @@ -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. /// @@ -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 @@ -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. /// @@ -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:?}"); /// } /// ``` @@ -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. /// @@ -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:?}"); /// } /// ``` @@ -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()); /// } /// ``` @@ -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()); /// } /// ``` @@ -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()); /// } /// ``` @@ -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()); /// } /// ``` @@ -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()); /// } /// ```