Use Box<dyn Iterator> as return type of functions

This commit is contained in:
Your Name 2021-10-11 00:11:44 +01:00
parent 3a81585d51
commit 33b5975f93
8 changed files with 29 additions and 41 deletions

View File

@ -1,6 +1,5 @@
use crate::color::Rgb;
use serde::{Deserialize, Serialize};
use std::collections::vec_deque;
pub mod collide;
pub mod fade;
@ -17,6 +16,8 @@ pub use moving_rainbow::{MovingRainbow, MovingRainbowParams};
pub use orb::{Orb, OrbParams};
pub use solid::{Solid, SolidParams};
pub type ColorIterator<'a> = Box<dyn Iterator<Item = &'a Rgb> + 'a>;
pub trait FormRender {
fn render(&self) -> String;
}
@ -193,7 +194,7 @@ impl Parameters {
pub trait Pattern: std::fmt::Debug + Send + Sync {
fn init(&mut self, num_lights: u16) -> Result<(), ()>;
fn step(&mut self) -> Result<bool, ()>;
fn get_strip(&self) -> vec_deque::Iter<Rgb>;
fn get_strip<'a>(&'a self) -> ColorIterator;
}
// #[cfg(test)]

View File

@ -1,7 +1,7 @@
use super::{FormRender, InputRender, Pattern};
use super::{ColorIterator, FormRender, InputRender, Pattern};
use crate::color::{self, Rgb};
use serde::{Deserialize, Serialize};
use std::collections::{vec_deque, VecDeque};
use std::collections::VecDeque;
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct CollideParams {
@ -183,7 +183,7 @@ impl Pattern for Collide {
Ok(())
}
fn get_strip(&self) -> vec_deque::Iter<Rgb> {
self.lights_buf.iter()
fn get_strip<'a>(&'a self) -> ColorIterator<'a> {
Box::new(self.lights_buf.iter())
}
}

View File

@ -1,10 +1,7 @@
use super::{FormRender, InputRender, Pattern};
use super::{ColorIterator, FormRender, InputRender, Pattern};
use crate::color::{self, merge_colors_u8, Rgb};
use serde::{Deserialize, Serialize};
use std::{
collections::{vec_deque, VecDeque},
iter,
};
use std::{collections::VecDeque, iter};
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct FadeParams {
@ -78,7 +75,7 @@ impl Pattern for Fade {
self.lights_buf = VecDeque::from(vec![color::BLACK; self.num_lights.into()]);
Ok(())
}
fn get_strip(&self) -> vec_deque::Iter<Rgb> {
self.lights_buf.iter()
fn get_strip<'a>(&'a self) -> ColorIterator {
Box::new(self.lights_buf.iter())
}
}

View File

@ -1,10 +1,7 @@
use super::{FormRender, InputRender, Pattern};
use super::{ColorIterator, FormRender, InputRender, Pattern};
use crate::color::{self, Rgb};
use serde::{Deserialize, Serialize};
use std::{
collections::{vec_deque, VecDeque},
iter,
};
use std::{collections::VecDeque, iter};
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct FlashingParams {
@ -90,7 +87,7 @@ impl Pattern for Flashing {
Ok(())
}
fn get_strip(&self) -> vec_deque::Iter<Rgb> {
self.lights_buf.iter()
fn get_strip<'a>(&'a self) -> ColorIterator {
Box::new(self.lights_buf.iter())
}
}

View File

@ -1,7 +1,7 @@
use super::{FormRender, InputRender, Pattern};
use super::{ColorIterator, FormRender, InputRender, Pattern};
use crate::color::{self, Rgb};
use serde::{Deserialize, Serialize};
use std::collections::{vec_deque, VecDeque};
use std::collections::VecDeque;
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct MovingPixelParams {
@ -68,7 +68,7 @@ impl Pattern for MovingPixel {
*self.lights_buf.get_mut(0).ok_or(())? = self.color;
Ok(())
}
fn get_strip(&self) -> vec_deque::Iter<Rgb> {
self.lights_buf.iter()
fn get_strip<'a>(&'a self) -> ColorIterator {
Box::new(self.lights_buf.iter())
}
}

View File

@ -1,11 +1,7 @@
use super::{FormRender, InputRender, Pattern};
use super::{ColorIterator, FormRender, InputRender, Pattern};
use crate::color::{self, Rgb, RAINBOW};
use serde::{Deserialize, Serialize};
use std::{
collections::{vec_deque, VecDeque},
convert::TryFrom,
iter,
};
use std::{collections::VecDeque, convert::TryFrom, iter};
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct MovingRainbowParams {

View File

@ -1,10 +1,7 @@
use super::{FormRender, InputRender, Pattern};
use super::{ColorIterator, FormRender, InputRender, Pattern};
use crate::color::{self, Rgb};
use serde::{Deserialize, Serialize};
use std::{
collections::{vec_deque, VecDeque},
iter,
};
use std::{collections::VecDeque, iter};
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct OrbParams {
@ -127,7 +124,7 @@ impl Pattern for Orb {
Ok(())
}
fn get_strip(&self) -> vec_deque::Iter<Rgb> {
self.lights_buf.iter()
fn get_strip<'a>(&'a self) -> ColorIterator {
Box::new(self.lights_buf.iter())
}
}

View File

@ -1,7 +1,7 @@
use super::{FormRender, InputRender, Pattern};
use super::{ColorIterator, FormRender, InputRender, Pattern};
use crate::color::{self, Rgb};
use serde::{Deserialize, Serialize};
use std::collections::{vec_deque, VecDeque};
use std::collections::VecDeque;
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct SolidParams {
@ -57,7 +57,7 @@ impl Pattern for Solid {
Ok(())
}
fn get_strip(&self) -> vec_deque::Iter<Rgb> {
self.lights_buf.iter()
fn get_strip<'a>(&'a self) -> ColorIterator {
Box::new(self.lights_buf.iter())
}
}