rw lock write

This commit is contained in:
jane 2021-12-26 21:32:42 -05:00
parent 3a00c4bb09
commit dfa638d9b4

View file

@ -3,11 +3,12 @@ use std::{
vec::Vec, vec::Vec,
string::String string::String
}; };
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 [RawColor]); fn execute(&self, values: &'a mut RwLockWriteGuard<'_, [RawColor]>);
fn parse(args: Vec<String>) -> Result<Self, ParseError> where Self: Sized; fn parse(args: Vec<String>) -> Result<Self, ParseError> where Self: Sized;
} }
@ -24,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 [RawColor]) {} fn execute(&self, values: &'a mut RwLockWriteGuard<'_, [RawColor]>)) {}
fn parse(args: Vec<String>) -> Result<Self, ParseError> { fn parse(args: Vec<String>) -> Result<Self, ParseError> {
Ok(Unit {}) Ok(Unit {})
} }
@ -37,9 +38,9 @@ struct Value {
} }
impl<'a> Pattern<'a> for Value { impl<'a> Pattern<'a> for Value {
fn execute(&self, values: &'a mut [RawColor]) { fn execute(&self, values: &'a mut RwLockWriteGuard<'_, [RawColor]>)) {
for i in 0..crate::LED_SIZE { for i in 0..crate::LED_SIZE {
let color: RawColor = values[i]; let color: RawColor = *values[i];
if self.r.is_some() { if self.r.is_some() {
color[0] = self.r.unwrap(); color[0] = self.r.unwrap();
} }
@ -49,7 +50,7 @@ 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; *values[i] = color;
} }
} }
fn parse(args: Vec<String>) -> Result<Self, ParseError> { fn parse(args: Vec<String>) -> Result<Self, ParseError> {