diff --git a/crates/servicepoint/src/brightness.rs b/crates/servicepoint/src/brightness.rs index 5401a9f..ad07aa6 100644 --- a/crates/servicepoint/src/brightness.rs +++ b/crates/servicepoint/src/brightness.rs @@ -77,8 +77,8 @@ impl From for Vec { } } -impl From for PrimitiveGrid { - fn from(value: PrimitiveGrid) -> Self { +impl From<&BrightnessGrid> for PrimitiveGrid { + fn from(value: &PrimitiveGrid) -> Self { let u8s = value .iter() .map(|brightness| (*brightness).into()) @@ -109,3 +109,33 @@ impl Distribution for Standard { Brightness(rng.gen_range(Brightness::MIN.0..=Brightness::MAX.0)) } } + +#[cfg(test)] +mod tests { + use super::*; + use crate::DataRef; + + #[test] + fn brightness_from_u8() { + assert_eq!(Err(100), Brightness::try_from(100)); + assert_eq!(Ok(Brightness(1)), Brightness::try_from(1)) + } + + #[test] + #[cfg(feature = "rand")] + fn rand_brightness() { + let mut rng = rand::thread_rng(); + for _ in 0..100 { + let _: Brightness = rng.gen(); + } + } + + #[test] + fn to_u8_grid() { + let mut grid = BrightnessGrid::new(2, 2); + grid.set(1, 0, Brightness::MIN); + grid.set(0, 1, Brightness::MAX); + let actual = PrimitiveGrid::from(&grid); + assert_eq!(actual.data_ref(), &[11, 0, 11, 11]); + } +} diff --git a/crates/servicepoint/src/packet.rs b/crates/servicepoint/src/packet.rs index a8f0831..e53a7a1 100644 --- a/crates/servicepoint/src/packet.rs +++ b/crates/servicepoint/src/packet.rs @@ -331,7 +331,7 @@ mod tests { payload: vec![42u8; 23], }; let data: Vec = p.into(); - let p = Packet::try_from(&*data).unwrap(); + let p = Packet::try_from(data).unwrap(); assert_eq!( p, Packet {