lifetimes must go

This commit is contained in:
jane 2021-12-28 17:37:02 -05:00
parent e94d186001
commit e67f84a4ce
2 changed files with 15 additions and 9 deletions

View file

@ -8,12 +8,14 @@ use std::{thread, time};
pub const LED_SIZE: usize = 450; //450
pub const BRIGHTNESS: u8 = 150;
pub const LOOP_WAIT: u64 = 250;
fn main() {
let p: RawColor = [0, 0, 0, 0];
let lock = Arc::new(RwLock::new([p; LED_SIZE]));
let mut pattern: Vec<Box<dyn Pattern>> = Vec::new();
let read = Arc::clone(&lock);
/// light management
thread::spawn(move || {
let mut controller: Controller = ControllerBuilder::new()
.channel(0, ChannelBuilder::new()
@ -30,6 +32,8 @@ fn main() {
run_lights(&mut controller, &lights).expect("Error running lights controller.");
}
});
/// pattern parsing
let mul_string = format_multiline("unit");
let res: Result<Vec<Box<dyn Pattern>>, ParseError> = mul_string.iter()
.map(|x: &Vec<String>| parse_line((*x).clone()))
@ -37,8 +41,10 @@ fn main() {
if res.is_ok() {
pattern = res.unwrap();
}
/// pattern management
loop {
thread::sleep(time::Duration::from_millis(250));
thread::sleep(time::Duration::from_millis(LOOP_WAIT));
for p in &pattern {
let c_lock = Arc::clone(&lock);
let mut lights = c_lock.write().unwrap();

View file

@ -7,12 +7,12 @@ use std::sync::RwLockWriteGuard;
pub type ParseError = Box<dyn std::error::Error>;
pub trait Pattern<'a> {
fn execute(&self, values: &'a mut RwLockWriteGuard<'_, [RawColor; crate::LED_SIZE]>);
pub trait Pattern {
fn execute(&self, values: &mut RwLockWriteGuard<'_, [RawColor; crate::LED_SIZE]>);
fn parse(args: Vec<String>) -> Result<Self, ParseError> where Self: Sized;
}
impl<'a> TryFrom<Vec<String>> for Box<dyn Pattern<'a>> {
impl TryFrom<Vec<String>> for Box<dyn Pattern> {
type Error = ParseError;
fn try_from(s: Vec<String>) -> Result<Self, Self::Error> {
match s[0].as_str() {
@ -24,8 +24,8 @@ impl<'a> TryFrom<Vec<String>> for Box<dyn Pattern<'a>> {
}
struct Unit;
impl<'a> Pattern<'a> for Unit {
fn execute(&self, _values: &'a mut RwLockWriteGuard<'_, [RawColor; crate::LED_SIZE]>) {}
impl Pattern for Unit {
fn execute(&self, _values: &mut RwLockWriteGuard<'_, [RawColor; crate::LED_SIZE]>) {}
fn parse(_args: Vec<String>) -> Result<Self, ParseError> {
Ok(Unit {})
}
@ -37,8 +37,8 @@ struct Value {
b: Option<u8>
}
impl<'a> Pattern<'a> for Value {
fn execute(&self, values: &'a mut RwLockWriteGuard<'_, [RawColor; crate::LED_SIZE]>) {
impl Pattern for Value {
fn execute(&self, values: &mut RwLockWriteGuard<'_, [RawColor; crate::LED_SIZE]>) {
for i in 0..crate::LED_SIZE {
let mut color: RawColor = (*values)[i];
if self.r.is_some() {
@ -96,7 +96,7 @@ impl<'a> Pattern<'a> for Value {
}
}
pub fn parse_line<'a>(v: Vec<String>) -> Result<Box<dyn Pattern<'a>>, ParseError> {
pub fn parse_line(v: Vec<String>) -> Result<Box<dyn Pattern>, ParseError> {
v.try_into()
}