deref syntax?

This commit is contained in:
jane 2021-12-26 21:36:05 -05:00
parent 455fff23cb
commit b96f0d80db
1 changed files with 8 additions and 6 deletions

View File

@ -8,7 +8,7 @@ use std::sync::RwLockWriteGuard;
type ParseError = Box<dyn std::error::Error>; type ParseError = Box<dyn std::error::Error>;
pub trait Pattern<'a> { pub trait Pattern<'a> {
fn execute(&self, values: &'a mut RwLockWriteGuard<'_, [RawColor]>); fn execute(&self, values: &'a mut RwLockWriteGuard<'_, [RawColor; crate::LED_SIZE]>);
fn parse(args: Vec<String>) -> Result<Self, ParseError> where Self: Sized; fn parse(args: Vec<String>) -> Result<Self, ParseError> where Self: Sized;
} }
@ -25,7 +25,7 @@ impl<'a> TryFrom<Vec<String>> for Box<dyn Pattern<'a>> {
struct Unit; struct Unit;
impl<'a> Pattern<'a> for Unit { impl<'a> Pattern<'a> for Unit {
fn execute(&self, values: &'a mut RwLockWriteGuard<'_, [RawColor]>) {} fn execute(&self, values: &'a mut RwLockWriteGuard<'_, [RawColor; crate::LED_SIZE]>) {}
fn parse(args: Vec<String>) -> Result<Self, ParseError> { fn parse(args: Vec<String>) -> Result<Self, ParseError> {
Ok(Unit {}) Ok(Unit {})
} }
@ -38,9 +38,10 @@ struct Value {
} }
impl<'a> Pattern<'a> for Value { impl<'a> Pattern<'a> for Value {
fn execute(&self, values: &'a mut RwLockWriteGuard<'_, [RawColor]>) { fn execute(&self, values: &'a mut RwLockWriteGuard<'_, [RawColor; crate::LED_SIZE]>) {
let mut v = *values;
for i in 0..crate::LED_SIZE { for i in 0..crate::LED_SIZE {
let color: RawColor = *values[i]; let mut color: RawColor = v[i];
if self.r.is_some() { if self.r.is_some() {
color[0] = self.r.unwrap(); color[0] = self.r.unwrap();
} }
@ -50,8 +51,9 @@ impl<'a> Pattern<'a> for Value {
if self.b.is_some() { if self.b.is_some() {
color[0] = self.b.unwrap(); color[0] = self.b.unwrap();
} }
*values[i] = color; v[i] = color;
} }
*values = v;
} }
fn parse(args: Vec<String>) -> Result<Self, ParseError> { fn parse(args: Vec<String>) -> Result<Self, ParseError> {
let param1 = args[1].parse::<u8>(); let param1 = args[1].parse::<u8>();
@ -95,7 +97,7 @@ impl<'a> Pattern<'a> for Value {
} }
} }
pub fn parse_line(v: Vec<String>) -> Result<Box<dyn Pattern<'static>>, ParseError> { pub fn parse_line(v: Vec<String>) -> Result<Box<dyn Pattern<'_>>, ParseError> {
let res: Result<Box<dyn Pattern>, ParseError> = v.try_into(); let res: Result<Box<dyn Pattern>, ParseError> = v.try_into();
res res
} }