From e56aeb23b921515e4b4e9588646cf3479e642db3 Mon Sep 17 00:00:00 2001 From: Stephen Hunt Date: Fri, 22 Mar 2024 19:03:52 -0700 Subject: [PATCH 1/2] Rename iterators for Chain and Device types --- src/ascii/chain.rs | 26 +++++++++--------- src/ascii/chain/iter.rs | 60 +++++++++++++++++++++++------------------ 2 files changed, 47 insertions(+), 39 deletions(-) diff --git a/src/ascii/chain.rs b/src/ascii/chain.rs index af9ef494..85787436 100644 --- a/src/ascii/chain.rs +++ b/src/ascii/chain.rs @@ -13,7 +13,7 @@ use crate::{ shared::{Shared, SharedMut}, }; use info::ChainInfo; -use iter::{ChainIntoIter, ChainIter, DeviceIntoIter, DeviceIter}; +use iter::{IntoIterAxes, IntoIterDevices, IterAxes, IterDevices}; use setting::{AxisSettings, DeviceSettings}; use std::{ cell::RefCell, @@ -110,8 +110,8 @@ where } /// Get an iterator over the devices in the chain. - pub fn iter(&self) -> ChainIter<'a, B, P> { - ChainIter::new(self.port.clone(), self.info.clone()) + pub fn iter(&self) -> IterDevices<'a, B, P> { + IterDevices::new(self.port.clone(), self.info.clone()) } /// Get the [`Device`] at the specified address. @@ -138,10 +138,10 @@ where P: SharedMut>, { type Item = Device<'a, B, P>; - type IntoIter = ChainIntoIter<'a, B, P>; + type IntoIter = IntoIterDevices<'a, B, P>; fn into_iter(self) -> Self::IntoIter { - ChainIntoIter::new(self.port, self.info) + IntoIterDevices::new(self.port, self.info) } } @@ -150,10 +150,10 @@ where P: SharedMut>, { type Item = Device<'a, B, P>; - type IntoIter = ChainIter<'a, B, P>; + type IntoIter = IterDevices<'a, B, P>; fn into_iter(self) -> Self::IntoIter { - ChainIter::new(self.port.clone(), self.info.clone()) + IterDevices::new(self.port.clone(), self.info.clone()) } } @@ -231,8 +231,8 @@ where } /// Get an iterator over the axes of the device. - pub fn iter(&self) -> DeviceIter<'a, B, P> { - DeviceIter::new(self.port.clone(), self.info.clone(), self.address) + pub fn iter(&self) -> IterAxes<'a, B, P> { + IterAxes::new(self.port.clone(), self.info.clone(), self.address) } /// Get access to this device's settings. @@ -285,10 +285,10 @@ where P: SharedMut>, { type Item = Axis<'a, B, P>; - type IntoIter = DeviceIntoIter<'a, B, P>; + type IntoIter = IntoIterAxes<'a, B, P>; fn into_iter(self) -> Self::IntoIter { - DeviceIntoIter::new(self.port, self.info, self.address) + IntoIterAxes::new(self.port, self.info, self.address) } } @@ -297,10 +297,10 @@ where P: SharedMut>, { type Item = Axis<'a, B, P>; - type IntoIter = DeviceIter<'a, B, P>; + type IntoIter = IterAxes<'a, B, P>; fn into_iter(self) -> Self::IntoIter { - DeviceIter::new(self.port.clone(), self.info.clone(), self.address) + IterAxes::new(self.port.clone(), self.info.clone(), self.address) } } diff --git a/src/ascii/chain/iter.rs b/src/ascii/chain/iter.rs index 554efa50..7fea19bb 100644 --- a/src/ascii/chain/iter.rs +++ b/src/ascii/chain/iter.rs @@ -8,18 +8,20 @@ use crate::{ use std::num::NonZeroU8; /// An iterator of the devices in a [`Chain`](super::Chain). -pub struct ChainIter<'a, B, P: SharedMut>> { +/// +/// Created via [`Chain::iter()`](super::Chain::iter). +pub struct IterDevices<'a, B, P: SharedMut>> { port: P, info: P::Wrapper, next_index: usize, } -impl<'a, B, P> ChainIter<'a, B, P> +impl<'a, B, P> IterDevices<'a, B, P> where P: SharedMut>, { /// Create an new iterator over the devices in the chain. pub(super) fn new(port: P, info: P::Wrapper) -> Self { - ChainIter { + IterDevices { port, info, next_index: 0, @@ -27,16 +29,16 @@ where } } -impl<'a, B, P> std::fmt::Debug for ChainIter<'a, B, P> +impl<'a, B, P> std::fmt::Debug for IterDevices<'a, B, P> where P: SharedMut>, { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.debug_struct("ChainIter").finish_non_exhaustive() + f.debug_struct("IterDevices").finish_non_exhaustive() } } -impl<'a, B, P> std::iter::Iterator for ChainIter<'a, B, P> +impl<'a, B, P> std::iter::Iterator for IterDevices<'a, B, P> where P: SharedMut>, { @@ -64,30 +66,32 @@ where } /// A by-value iterator over the devices in a [`Chain`](super::Chain). -pub struct ChainIntoIter<'a, B, P: SharedMut>> { - iter: ChainIter<'a, B, P>, +/// +/// Created via [`Chain::into_iter()`](super::Chain::into_iter). +pub struct IntoIterDevices<'a, B, P: SharedMut>> { + iter: IterDevices<'a, B, P>, } -impl<'a, B, P> ChainIntoIter<'a, B, P> +impl<'a, B, P> IntoIterDevices<'a, B, P> where P: SharedMut>, { pub(super) fn new(port: P, info: P::Wrapper) -> Self { - ChainIntoIter { - iter: ChainIter::new(port, info), + IntoIterDevices { + iter: IterDevices::new(port, info), } } } -impl<'a, B, P> std::fmt::Debug for ChainIntoIter<'a, B, P> +impl<'a, B, P> std::fmt::Debug for IntoIterDevices<'a, B, P> where P: SharedMut>, { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.debug_struct("ChainIntoIter").finish_non_exhaustive() + f.debug_struct("IntoIterDevices").finish_non_exhaustive() } } -impl<'a, B, P> std::iter::Iterator for ChainIntoIter<'a, B, P> +impl<'a, B, P> std::iter::Iterator for IntoIterDevices<'a, B, P> where P: SharedMut>, { @@ -98,19 +102,21 @@ where } /// An iterator over the axes of a [`Device`]. -pub struct DeviceIter<'a, B, P: SharedMut>> { +/// +/// Created via [`Device::iter()`]. +pub struct IterAxes<'a, B, P: SharedMut>> { port: P, info: P::Wrapper, address: NonZeroU8, next_axis_index: usize, } -impl<'a, B, P> DeviceIter<'a, B, P> +impl<'a, B, P> IterAxes<'a, B, P> where P: SharedMut>, { pub(super) fn new(port: P, info: P::Wrapper, address: NonZeroU8) -> Self { - DeviceIter { + IterAxes { port, info, address, @@ -119,7 +125,7 @@ where } } -impl<'a, B, P> std::iter::Iterator for DeviceIter<'a, B, P> +impl<'a, B, P> std::iter::Iterator for IterAxes<'a, B, P> where P: SharedMut>, { @@ -154,35 +160,37 @@ where } } -impl<'a, B, P> std::fmt::Debug for DeviceIter<'a, B, P> +impl<'a, B, P> std::fmt::Debug for IterAxes<'a, B, P> where P: SharedMut>, { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.debug_struct("DeviceIter") + f.debug_struct("IterAxes") .field("address", &self.address) .finish() } } /// A by-value iterator over the axes of a [`Device`]. +/// +/// Created via [`Device::into_iter()`]. #[derive(Debug)] -pub struct DeviceIntoIter<'a, B, P: SharedMut>> { - iter: DeviceIter<'a, B, P>, +pub struct IntoIterAxes<'a, B, P: SharedMut>> { + iter: IterAxes<'a, B, P>, } -impl<'a, B, P> DeviceIntoIter<'a, B, P> +impl<'a, B, P> IntoIterAxes<'a, B, P> where P: SharedMut>, { pub(super) fn new(port: P, info: P::Wrapper, address: NonZeroU8) -> Self { - DeviceIntoIter { - iter: DeviceIter::new(port, info, address), + IntoIterAxes { + iter: IterAxes::new(port, info, address), } } } -impl<'a, B, P> std::iter::Iterator for DeviceIntoIter<'a, B, P> +impl<'a, B, P> std::iter::Iterator for IntoIterAxes<'a, B, P> where P: SharedMut>, { From b7944d33c33e0fccc5bec2cb8696464fb47b9786 Mon Sep 17 00:00:00 2001 From: Stephen Hunt Date: Fri, 22 Mar 2024 19:03:53 -0700 Subject: [PATCH 2/2] impl IntoIterator for &mut Chain and &mut Device --- src/ascii/chain.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/ascii/chain.rs b/src/ascii/chain.rs index 85787436..6730aff3 100644 --- a/src/ascii/chain.rs +++ b/src/ascii/chain.rs @@ -157,6 +157,18 @@ where } } +impl<'a, B, P> std::iter::IntoIterator for &mut Chain<'a, B, P> +where + P: SharedMut>, +{ + type Item = Device<'a, B, P>; + type IntoIter = IterDevices<'a, B, P>; + + fn into_iter(self) -> Self::IntoIter { + IterDevices::new(self.port.clone(), self.info.clone()) + } +} + impl Chain<'static, Port<'static, Serial>> { /// Get a [`ChainOptions`] to customize the creation of a new [`Chain`]. pub fn options() -> ChainOptions { @@ -304,6 +316,18 @@ where } } +impl<'a, B, P> std::iter::IntoIterator for &mut Device<'a, B, P> +where + P: SharedMut>, +{ + type Item = Axis<'a, B, P>; + type IntoIter = IterAxes<'a, B, P>; + + fn into_iter(self) -> Self::IntoIter { + IterAxes::new(self.port.clone(), self.info.clone(), self.address) + } +} + /// Represents a single axis on a device. #[derive(Debug)] pub struct Axis<'a, B, P: SharedMut>> {