use core::convert::Infallible;
use rgb::RGB8;
use crate::AsyncStatusLed;
use crate::StatusLed;
#[derive(Copy, Clone, Default, Debug)]
pub struct NoLed;
impl StatusLed for NoLed {
type Error = Infallible;
fn set_color(&mut self, _color: RGB8) -> Result<(), Self::Error> {
Ok(())
}
}
impl AsyncStatusLed for NoLed {
type Error = Infallible;
async fn set_color(&mut self, _color: RGB8) -> Result<(), Self::Error> {
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn set_color_always_returns_ok() {
let mut led = NoLed;
assert!(StatusLed::set_color(&mut led, RGB8::new(255, 0, 0)).is_ok());
assert!(StatusLed::set_color(&mut led, RGB8::new(0, 255, 0)).is_ok());
assert!(StatusLed::set_color(&mut led, RGB8::new(0, 0, 255)).is_ok());
assert!(StatusLed::set_color(&mut led, RGB8::new(0, 0, 0)).is_ok());
assert!(StatusLed::set_color(&mut led, RGB8::new(255, 255, 255)).is_ok());
}
#[test]
fn error_type_is_infallible() {
let mut led = NoLed;
let _: () = StatusLed::set_color(&mut led, RGB8::new(128, 64, 32)).unwrap();
}
fn block_on_ready<F: core::future::Future>(f: F) -> F::Output {
use core::pin::pin;
use core::task::{Context, Poll, RawWaker, RawWakerVTable, Waker};
const VTABLE: RawWakerVTable = RawWakerVTable::new(
|_| RawWaker::new(core::ptr::null(), &VTABLE),
|_| {},
|_| {},
|_| {},
);
let waker = unsafe { Waker::from_raw(RawWaker::new(core::ptr::null(), &VTABLE)) };
let mut cx = Context::from_waker(&waker);
let mut f = pin!(f);
match f.as_mut().poll(&mut cx) {
Poll::Ready(v) => v,
Poll::Pending => panic!(
"block_on_ready: future returned Poll::Pending, but the contract \
requires it to complete on the first poll (no real executor here)"
),
}
}
#[test]
fn async_set_color_always_returns_ok() {
let mut led = NoLed;
block_on_ready(async {
assert!(AsyncStatusLed::set_color(&mut led, RGB8::new(255, 0, 0))
.await
.is_ok());
assert!(AsyncStatusLed::set_color(&mut led, RGB8::new(0, 0, 0))
.await
.is_ok());
});
}
#[test]
fn async_error_type_is_infallible() {
let mut led = NoLed;
block_on_ready(async {
let _: () = AsyncStatusLed::set_color(&mut led, RGB8::new(128, 64, 32))
.await
.unwrap();
});
}
}