From e2952cb6147c67397b7ab75d93c487c1d472d4c4 Mon Sep 17 00:00:00 2001 From: Austen Adler Date: Thu, 12 Aug 2021 21:06:22 -0400 Subject: [PATCH] Return VecDeque Iter instead of slice in case VecDeque is used --- src/color.rs | 2 +- src/pattern.rs | 19 ++++++++++--------- src/pattern/collide.rs | 14 ++++++++------ src/pattern/fade.rs | 19 ++++++++++++------- src/pattern/moving_pixel.rs | 14 ++++++++------ src/pattern/moving_rainbow.rs | 14 +++++++------- src/pattern/orb.rs | 9 +++++---- src/pattern/solid.rs | 14 ++++++++------ src/strip.rs | 1 - src/webui.rs | 5 ++--- 10 files changed, 61 insertions(+), 50 deletions(-) diff --git a/src/color.rs b/src/color.rs index 5ebae4c..c79e6ce 100644 --- a/src/color.rs +++ b/src/color.rs @@ -25,7 +25,7 @@ impl FromStr for Rgb { type Err = ParseIntError; fn from_str(s: &str) -> Result { - if let [r, g, b] = s.split(" ").collect::>().as_slice() { + if let [r, g, b] = s.split(' ').collect::>().as_slice() { return Ok(Self(r.parse::()?, g.parse::()?, b.parse::()?)); } diff --git a/src/pattern.rs b/src/pattern.rs index e8fe8ba..d70ea52 100644 --- a/src/pattern.rs +++ b/src/pattern.rs @@ -1,5 +1,6 @@ use crate::color::Rgb; use serde::Deserialize; +use std::collections::vec_deque; pub mod collide; pub mod fade; @@ -15,7 +16,7 @@ pub use orb::Orb; pub use solid::Solid; #[derive(Deserialize, Debug)] -pub enum PatternParameters { +pub enum Parameters { Collide(Rgb, Rgb, Rgb), Fade((Rgb,)), MovingPixel((Rgb,)), @@ -24,15 +25,15 @@ pub enum PatternParameters { Solid((Rgb,)), } -impl PatternParameters { +impl Parameters { pub fn to_pattern(&self) -> Box { match self { - PatternParameters::Collide(l, r, c) => Box::new(Collide::new(*l, *r, *c)), - PatternParameters::Fade((c,)) => Box::new(Fade::new(*c)), - PatternParameters::MovingPixel((c,)) => Box::new(MovingPixel::new(*c)), - PatternParameters::MovingRainbow(w, f) => Box::new(MovingRainbow::new(*w, *f)), - PatternParameters::Orb(c, x, y) => Box::new(Orb::new(*c, *x, *y)), - PatternParameters::Solid((c,)) => Box::new(Solid::new(*c)), + Self::Collide(l, r, c) => Box::new(Collide::new(*l, *r, *c)), + Self::Fade((c,)) => Box::new(Fade::new(*c)), + Self::MovingPixel((c,)) => Box::new(MovingPixel::new(*c)), + Self::MovingRainbow(w, f) => Box::new(MovingRainbow::new(*w, *f)), + Self::Orb(c, x, y) => Box::new(Orb::new(*c, *x, *y)), + Self::Solid((c,)) => Box::new(Solid::new(*c)), } } } @@ -40,7 +41,7 @@ impl PatternParameters { pub trait Pattern: std::fmt::Debug + Send + Sync { fn init(&mut self, num_lights: u16) -> Result<(), ()>; fn step(&mut self) -> Result; - fn get_strip(&self) -> &[Rgb]; + fn get_strip(&self) -> vec_deque::Iter; } // #[cfg(test)] diff --git a/src/pattern/collide.rs b/src/pattern/collide.rs index 3366bbf..0136449 100644 --- a/src/pattern/collide.rs +++ b/src/pattern/collide.rs @@ -1,5 +1,7 @@ use super::Pattern; use crate::color::{self, Rgb}; +use std::collections::vec_deque; +use std::collections::VecDeque; #[derive(Clone, Debug)] pub struct Collide { @@ -14,10 +16,10 @@ pub struct Collide { offset: u16, previous_offset: u16, increase_offset: bool, - lights_buf: Vec, + lights_buf: VecDeque, } impl Collide { - pub const fn new(left_color: Rgb, right_color: Rgb, conjoined_color: Rgb) -> Self { + pub fn new(left_color: Rgb, right_color: Rgb, conjoined_color: Rgb) -> Self { Self { num_lights: 0, left_color, @@ -30,7 +32,7 @@ impl Collide { previous_offset: 0, offset: 0, increase_offset: true, - lights_buf: vec![], + lights_buf: VecDeque::new(), } } } @@ -114,7 +116,7 @@ impl Pattern for Collide { if self.num_lights < 3 { return Err(()); } - self.lights_buf = vec![color::BLACK; self.num_lights.into()]; + self.lights_buf = VecDeque::from(vec![color::BLACK; self.num_lights.into()]); if self.num_lights.rem_euclid(2) == 0 { self.conjoined_bounds = ( self.num_lights @@ -147,7 +149,7 @@ impl Pattern for Collide { Ok(()) } - fn get_strip(&self) -> &[Rgb] { - &self.lights_buf + fn get_strip(&self) -> vec_deque::Iter { + self.lights_buf.iter() } } diff --git a/src/pattern/fade.rs b/src/pattern/fade.rs index 902ac25..a51cc47 100644 --- a/src/pattern/fade.rs +++ b/src/pattern/fade.rs @@ -1,5 +1,7 @@ use super::Pattern; use crate::color::{self, Rgb}; +use std::collections::vec_deque; +use std::collections::VecDeque; #[derive(Clone, Debug)] pub struct Fade { @@ -7,16 +9,16 @@ pub struct Fade { step: u8, direction: bool, num_lights: u16, - lights_buf: Vec, + lights_buf: VecDeque, } impl Fade { - pub const fn new(color: Rgb) -> Self { + pub fn new(color: Rgb) -> Self { Self { color, step: 0, direction: true, num_lights: 1, - lights_buf: vec![], + lights_buf: VecDeque::new(), } } } @@ -33,7 +35,10 @@ impl Pattern for Fade { } self.step = self.step.saturating_sub(1); } - self.lights_buf = vec![Rgb(self.step, self.step, self.step); self.num_lights.into()]; + self.lights_buf = VecDeque::from(vec![ + Rgb(self.step, self.step, self.step); + self.num_lights.into() + ]); Ok(true) } fn init(&mut self, num_lights: u16) -> Result<(), ()> { @@ -43,10 +48,10 @@ impl Pattern for Fade { self.step = 0; self.direction = true; self.num_lights = num_lights; - self.lights_buf = vec![color::BLACK; self.num_lights.into()]; + self.lights_buf = VecDeque::from(vec![color::BLACK; self.num_lights.into()]); Ok(()) } - fn get_strip(&self) -> &[Rgb] { - &self.lights_buf + fn get_strip(&self) -> vec_deque::Iter { + self.lights_buf.iter() } } diff --git a/src/pattern/moving_pixel.rs b/src/pattern/moving_pixel.rs index 94e4945..baea193 100644 --- a/src/pattern/moving_pixel.rs +++ b/src/pattern/moving_pixel.rs @@ -1,22 +1,24 @@ use super::Pattern; use crate::color::{self, Rgb}; +use std::collections::vec_deque; +use std::collections::VecDeque; #[derive(Clone, Debug)] pub struct MovingPixel { color: Rgb, num_lights: u16, step: u16, - lights_buf: Vec, + lights_buf: VecDeque, } impl MovingPixel { - pub const fn new(color: Rgb) -> Self { + pub fn new(color: Rgb) -> Self { Self { color, step: 0, // TODO: Better initialization num_lights: 1, - lights_buf: vec![], + lights_buf: VecDeque::new(), } } } @@ -38,11 +40,11 @@ impl Pattern for MovingPixel { self.step = 0; self.num_lights = num_lights; // Set the strip to black except for one pixel - self.lights_buf = vec![color::BLACK; num_lights.into()]; + self.lights_buf = VecDeque::from(vec![color::BLACK; num_lights.into()]); *self.lights_buf.get_mut(0).ok_or(())? = self.color; Ok(()) } - fn get_strip(&self) -> &[Rgb] { - &self.lights_buf + fn get_strip(&self) -> vec_deque::Iter { + self.lights_buf.iter() } } diff --git a/src/pattern/moving_rainbow.rs b/src/pattern/moving_rainbow.rs index 7da0ef2..adfb023 100644 --- a/src/pattern/moving_rainbow.rs +++ b/src/pattern/moving_rainbow.rs @@ -1,20 +1,20 @@ use super::Pattern; use crate::color::{Rgb, RAINBOW}; +use std::collections::vec_deque; +use std::collections::VecDeque; use std::convert::TryFrom; use std::iter; -//use std::collections::VecDeque; -//TODO Use a VecDeque #[derive(Clone, Debug)] pub struct MovingRainbow { - lights_buf: Vec, + lights_buf: VecDeque, width: u8, forward: bool, } impl MovingRainbow { - pub const fn new(width: u8, forward: bool) -> Self { + pub fn new(width: u8, forward: bool) -> Self { Self { - lights_buf: vec![], + lights_buf: VecDeque::new(), width, forward, } @@ -57,7 +57,7 @@ impl Pattern for MovingRainbow { Ok(()) } - fn get_strip(&self) -> &[Rgb] { - &self.lights_buf + fn get_strip(&self) -> vec_deque::Iter { + self.lights_buf.iter() } } diff --git a/src/pattern/orb.rs b/src/pattern/orb.rs index 32717c9..9bfc0e8 100644 --- a/src/pattern/orb.rs +++ b/src/pattern/orb.rs @@ -1,7 +1,8 @@ use super::Pattern; use crate::color::{self, Rgb}; -use std::iter; +use std::collections::vec_deque; use std::collections::VecDeque; +use std::iter; #[derive(Clone, Debug)] pub struct Orb { @@ -25,7 +26,7 @@ pub struct Orb { direction: bool, } impl Orb { - pub const fn new(color: Rgb, center_width: u8, backoff_width: u8) -> Self { + pub fn new(color: Rgb, center_width: u8, backoff_width: u8) -> Self { Self { lights_buf: VecDeque::new(), color, @@ -89,7 +90,7 @@ impl Pattern for Orb { Ok(()) } - fn get_strip(&self) -> &[Rgb] { - &self.lights_buf + fn get_strip(&self) -> vec_deque::Iter { + self.lights_buf.iter() } } diff --git a/src/pattern/solid.rs b/src/pattern/solid.rs index 53a4a07..71f455c 100644 --- a/src/pattern/solid.rs +++ b/src/pattern/solid.rs @@ -1,18 +1,20 @@ use super::Pattern; use crate::color::Rgb; +use std::collections::vec_deque; +use std::collections::VecDeque; #[derive(Clone, Debug)] pub struct Solid { color: Rgb, has_run: bool, - lights_buf: Vec, + lights_buf: VecDeque, } impl Solid { - pub const fn new(color: Rgb) -> Self { + pub fn new(color: Rgb) -> Self { Self { color, has_run: false, - lights_buf: vec![], + lights_buf: VecDeque::new(), } } } @@ -27,11 +29,11 @@ impl Pattern for Solid { return Err(()); } self.has_run = false; - self.lights_buf = vec![self.color; num_lights.into()]; + self.lights_buf = VecDeque::from(vec![self.color; num_lights.into()]); Ok(()) } - fn get_strip(&self) -> &[Rgb] { - &self.lights_buf + fn get_strip(&self) -> vec_deque::Iter { + self.lights_buf.iter() } } diff --git a/src/strip.rs b/src/strip.rs index 08c211f..6bcb505 100644 --- a/src/strip.rs +++ b/src/strip.rs @@ -70,7 +70,6 @@ impl LedStrip { self.pattern .get_strip() // .as_slice() - .iter() .take(self.config.num_lights.into()), ) .map(|c| c.to_tuple()) diff --git a/src/webui.rs b/src/webui.rs index c665c03..c0f5713 100644 --- a/src/webui.rs +++ b/src/webui.rs @@ -1,10 +1,9 @@ use crate::pattern; use crate::strip; -use actix_web::error::{ErrorBadRequest, JsonPayloadError}; +use actix_web::error::JsonPayloadError; use actix_web::web::JsonConfig; use actix_web::{post, web, App, HttpServer, Responder, Result}; use actix_web_static_files; -use pattern::PatternParameters; use std::io; use std::sync::{Arc, Mutex}; @@ -19,7 +18,7 @@ struct AppState { #[post("/setcolor")] async fn set_color( data: web::Data, - params: web::Json, + params: web::Json, ) -> Result { println!("Got params: {:?}", params); data.strip_tx