Remove slice panics with results

This commit is contained in:
Your Name 2021-08-02 00:56:27 +01:00
parent 307d9b88ab
commit cb14450d07
7 changed files with 31 additions and 25 deletions

View File

@ -14,7 +14,7 @@ pub use solid::Solid;
pub trait Pattern: std::fmt::Debug { pub trait Pattern: std::fmt::Debug {
fn init(&mut self, lights_buf: &mut Vec<RGB>, num_lights: u16) -> Result<(), ()>; fn init(&mut self, lights_buf: &mut Vec<RGB>, num_lights: u16) -> Result<(), ()>;
fn step(&mut self, lights_buf: &mut Vec<RGB>) -> bool; fn step(&mut self, lights_buf: &mut Vec<RGB>) -> Result<bool, ()>;
} }
// #[cfg(test)] // #[cfg(test)]

View File

@ -33,7 +33,7 @@ impl Collide {
} }
} }
impl Pattern for Collide { impl Pattern for Collide {
fn step(&mut self, lights_buf: &mut Vec<RGB>) -> bool { fn step(&mut self, lights_buf: &mut Vec<RGB>) -> Result<bool, ()> {
// TODO: Better range storage // TODO: Better range storage
// Set the left and right color // Set the left and right color
let colors = let colors =
@ -44,32 +44,38 @@ impl Pattern for Collide {
}; };
// Turn off the previous LED // Turn off the previous LED
lights_buf[usize::from(self.previous_offset)] = color::BLACK; *lights_buf
.get_mut(usize::from(self.previous_offset))
.ok_or(())? = color::BLACK;
if self.previous_offset if self.previous_offset
!= self != self
.num_lights .num_lights
.saturating_sub(1) .saturating_sub(1)
.saturating_sub(self.previous_offset) .saturating_sub(self.previous_offset)
{ {
lights_buf[usize::from( *lights_buf
.get_mut(usize::from(
self.num_lights self.num_lights
.saturating_sub(1) .saturating_sub(1)
.saturating_sub(self.previous_offset), .saturating_sub(self.previous_offset),
)] = color::BLACK; ))
.ok_or(())? = color::BLACK;
} }
// Set the color of the current offset // Set the color of the current offset
lights_buf[usize::from(self.offset)] = colors.0; *lights_buf.get_mut(usize::from(self.offset)).ok_or(())? = colors.0;
if self.offset if self.offset
!= self != self
.num_lights .num_lights
.saturating_sub(1) .saturating_sub(1)
.saturating_sub(self.offset) .saturating_sub(self.offset)
{ {
lights_buf[usize::from( *lights_buf
.get_mut(usize::from(
self.num_lights self.num_lights
.saturating_sub(1) .saturating_sub(1)
.saturating_sub(self.offset), .saturating_sub(self.offset),
)] = colors.1; ))
.ok_or(())? = colors.1;
} }
self.previous_offset = self.offset; self.previous_offset = self.offset;
@ -88,7 +94,7 @@ impl Pattern for Collide {
self.step = self.step.saturating_add(1).rem_euclid(self.step_max); self.step = self.step.saturating_add(1).rem_euclid(self.step_max);
true Ok(true)
} }
fn init(&mut self, lights_buf: &mut Vec<RGB>, num_lights: u16) -> Result<(), ()> { fn init(&mut self, lights_buf: &mut Vec<RGB>, num_lights: u16) -> Result<(), ()> {
// Reset changing parameters // Reset changing parameters

View File

@ -19,7 +19,7 @@ impl Fade {
} }
} }
impl Pattern for Fade { impl Pattern for Fade {
fn step(&mut self, lights_buf: &mut Vec<RGB>) -> bool { fn step(&mut self, lights_buf: &mut Vec<RGB>) -> Result<bool, ()> {
if self.direction { if self.direction {
if self.step == 254 { if self.step == 254 {
self.direction = !self.direction; self.direction = !self.direction;
@ -32,7 +32,7 @@ impl Pattern for Fade {
self.step = self.step.saturating_sub(1); self.step = self.step.saturating_sub(1);
} }
*lights_buf = vec![RGB(self.step, self.step, self.step); self.num_lights.into()]; *lights_buf = vec![RGB(self.step, self.step, self.step); self.num_lights.into()];
true Ok(true)
} }
fn init(&mut self, lights_buf: &mut Vec<RGB>, num_lights: u16) -> Result<(), ()> { fn init(&mut self, lights_buf: &mut Vec<RGB>, num_lights: u16) -> Result<(), ()> {
if num_lights < 1 { if num_lights < 1 {

View File

@ -17,14 +17,14 @@ impl MovingPixel {
} }
} }
impl Pattern for MovingPixel { impl Pattern for MovingPixel {
fn step(&mut self, lights_buf: &mut Vec<RGB>) -> bool { fn step(&mut self, lights_buf: &mut Vec<RGB>) -> Result<bool, ()> {
let len = self.num_lights; let len = self.num_lights;
lights_buf.swap( lights_buf.swap(
self.step.rem_euclid(len).into(), self.step.rem_euclid(len).into(),
self.step.saturating_add(1).saturating_mul(3).into(), self.step.saturating_add(1).saturating_mul(3).into(),
); );
self.step = self.step.wrapping_add(1); self.step = self.step.wrapping_add(1);
true Ok(true)
} }
fn init(&mut self, lights_buf: &mut Vec<RGB>, num_lights: u16) -> Result<(), ()> { fn init(&mut self, lights_buf: &mut Vec<RGB>, num_lights: u16) -> Result<(), ()> {
if num_lights < 1 { if num_lights < 1 {
@ -34,7 +34,7 @@ impl Pattern for MovingPixel {
self.num_lights = num_lights; self.num_lights = num_lights;
// Set the strip to black except for one pixel // Set the strip to black except for one pixel
*lights_buf = vec![color::BLACK; num_lights.into()]; *lights_buf = vec![color::BLACK; num_lights.into()];
lights_buf[0] = self.color; *lights_buf.get_mut(0).ok_or(())? = self.color;
Ok(()) Ok(())
} }
} }

View File

@ -9,9 +9,9 @@ impl MovingRainbow {
} }
} }
impl Pattern for MovingRainbow { impl Pattern for MovingRainbow {
fn step(&mut self, lights_buf: &mut Vec<RGB>) -> bool { fn step(&mut self, lights_buf: &mut Vec<RGB>) -> Result<bool, ()> {
lights_buf.rotate_right(1); lights_buf.rotate_right(1);
true Ok(true)
} }
fn init(&mut self, lights_buf: &mut Vec<RGB>, num_lights: u16) -> Result<(), ()> { fn init(&mut self, lights_buf: &mut Vec<RGB>, num_lights: u16) -> Result<(), ()> {
if num_lights < 1 { if num_lights < 1 {

View File

@ -15,10 +15,10 @@ impl Solid {
} }
} }
impl Pattern for Solid { impl Pattern for Solid {
fn step(&mut self, _lights_buf: &mut Vec<RGB>) -> bool { fn step(&mut self, _lights_buf: &mut Vec<RGB>) -> Result<bool, ()> {
let ret = !self.has_run; let ret = !self.has_run;
self.has_run = true; self.has_run = true;
ret Ok(ret)
} }
fn init(&mut self, lights_buf: &mut Vec<RGB>, num_lights: u16) -> Result<(), ()> { fn init(&mut self, lights_buf: &mut Vec<RGB>, num_lights: u16) -> Result<(), ()> {
if num_lights < 1 { if num_lights < 1 {

View File

@ -152,7 +152,7 @@ impl LEDStrip {
} }
} }
if self.pattern.step(&mut self.lights_buf) { if let Ok(true) = self.pattern.step(&mut self.lights_buf) {
self.write_buf(); self.write_buf();
} }