Skip to content

Commit

Permalink
Merge pull request #141 from stphnt/chain-iter-refactor
Browse files Browse the repository at this point in the history
Refactor iterators in ascii::chain::iter
  • Loading branch information
stphnt authored Mar 23, 2024
2 parents a9d3025 + b7944d3 commit c3031ca
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 39 deletions.
50 changes: 37 additions & 13 deletions src/ascii/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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.
Expand All @@ -138,10 +138,10 @@ where
P: SharedMut<Port<'a, B>>,
{
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)
}
}

Expand All @@ -150,10 +150,22 @@ where
P: SharedMut<Port<'a, B>>,
{
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())
}
}

impl<'a, B, P> std::iter::IntoIterator for &mut Chain<'a, B, P>
where
P: SharedMut<Port<'a, B>>,
{
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())
}
}

Expand Down Expand Up @@ -231,8 +243,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.
Expand Down Expand Up @@ -285,10 +297,10 @@ where
P: SharedMut<Port<'a, B>>,
{
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)
}
}

Expand All @@ -297,10 +309,22 @@ where
P: SharedMut<Port<'a, B>>,
{
type Item = Axis<'a, B, P>;
type IntoIter = DeviceIter<'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)
}
}

impl<'a, B, P> std::iter::IntoIterator for &mut Device<'a, B, P>
where
P: SharedMut<Port<'a, B>>,
{
type Item = Axis<'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)
}
}

Expand Down
60 changes: 34 additions & 26 deletions src/ascii/chain/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,37 @@ use crate::{
use std::num::NonZeroU8;

/// An iterator of the devices in a [`Chain`](super::Chain).
pub struct ChainIter<'a, B, P: SharedMut<Port<'a, B>>> {
///
/// Created via [`Chain::iter()`](super::Chain::iter).
pub struct IterDevices<'a, B, P: SharedMut<Port<'a, B>>> {
port: P,
info: P::Wrapper<ChainInfo>,
next_index: usize,
}
impl<'a, B, P> ChainIter<'a, B, P>
impl<'a, B, P> IterDevices<'a, B, P>
where
P: SharedMut<Port<'a, B>>,
{
/// Create an new iterator over the devices in the chain.
pub(super) fn new(port: P, info: P::Wrapper<ChainInfo>) -> Self {
ChainIter {
IterDevices {
port,
info,
next_index: 0,
}
}
}

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<Port<'a, B>>,
{
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<Port<'a, B>>,
{
Expand Down Expand Up @@ -64,30 +66,32 @@ where
}

/// A by-value iterator over the devices in a [`Chain`](super::Chain).
pub struct ChainIntoIter<'a, B, P: SharedMut<Port<'a, B>>> {
iter: ChainIter<'a, B, P>,
///
/// Created via [`Chain::into_iter()`](super::Chain::into_iter).
pub struct IntoIterDevices<'a, B, P: SharedMut<Port<'a, B>>> {
iter: IterDevices<'a, B, P>,
}
impl<'a, B, P> ChainIntoIter<'a, B, P>
impl<'a, B, P> IntoIterDevices<'a, B, P>
where
P: SharedMut<Port<'a, B>>,
{
pub(super) fn new(port: P, info: P::Wrapper<ChainInfo>) -> 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<Port<'a, B>>,
{
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<Port<'a, B>>,
{
Expand All @@ -98,19 +102,21 @@ where
}

/// An iterator over the axes of a [`Device`].
pub struct DeviceIter<'a, B, P: SharedMut<Port<'a, B>>> {
///
/// Created via [`Device::iter()`].
pub struct IterAxes<'a, B, P: SharedMut<Port<'a, B>>> {
port: P,
info: P::Wrapper<ChainInfo>,
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<Port<'a, B>>,
{
pub(super) fn new(port: P, info: P::Wrapper<ChainInfo>, address: NonZeroU8) -> Self {
DeviceIter {
IterAxes {
port,
info,
address,
Expand All @@ -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<Port<'a, B>>,
{
Expand Down Expand Up @@ -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<Port<'a, B>>,
{
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<Port<'a, B>>> {
iter: DeviceIter<'a, B, P>,
pub struct IntoIterAxes<'a, B, P: SharedMut<Port<'a, B>>> {
iter: IterAxes<'a, B, P>,
}

impl<'a, B, P> DeviceIntoIter<'a, B, P>
impl<'a, B, P> IntoIterAxes<'a, B, P>
where
P: SharedMut<Port<'a, B>>,
{
pub(super) fn new(port: P, info: P::Wrapper<ChainInfo>, 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<Port<'a, B>>,
{
Expand Down

0 comments on commit c3031ca

Please sign in to comment.