use crate::{DdcError, ScramblingStatus, SinkTmdsConfig, TmdsConfigReading, UpdateError};
#[allow(
async_fn_in_trait,
reason = "the plain trait serves single-threaded executors; Send futures are the twin's, SendTmdsScdc"
)]
pub trait TmdsScdc {
type Error: DdcError;
async fn write_tmds_config(&mut self, config: SinkTmdsConfig) -> Result<(), Self::Error>;
async fn update_tmds_config(
&mut self,
config: SinkTmdsConfig,
) -> Result<(), UpdateError<Self::Error>>;
async fn read_tmds_config(&mut self) -> Result<TmdsConfigReading, Self::Error>;
async fn read_scrambling_status(&mut self) -> Result<ScramblingStatus, Self::Error>;
async fn read_sink_version(&mut self) -> Result<u8, Self::Error>;
async fn write_source_version(&mut self, version: u8) -> Result<(), Self::Error>;
async fn clear_frl_rate(&mut self) -> Result<(), Self::Error>;
async fn clear_flt_update(&mut self) -> Result<bool, UpdateError<Self::Error>>;
}
pub trait SendTmdsScdc: Send {
type Error: DdcError + Send;
fn write_tmds_config(
&mut self,
config: SinkTmdsConfig,
) -> impl Future<Output = Result<(), Self::Error>> + Send;
fn update_tmds_config(
&mut self,
config: SinkTmdsConfig,
) -> impl Future<Output = Result<(), UpdateError<Self::Error>>> + Send;
fn read_tmds_config(
&mut self,
) -> impl Future<Output = Result<TmdsConfigReading, Self::Error>> + Send;
fn read_scrambling_status(
&mut self,
) -> impl Future<Output = Result<ScramblingStatus, Self::Error>> + Send;
fn read_sink_version(&mut self) -> impl Future<Output = Result<u8, Self::Error>> + Send;
fn write_source_version(
&mut self,
version: u8,
) -> impl Future<Output = Result<(), Self::Error>> + Send;
fn clear_frl_rate(&mut self) -> impl Future<Output = Result<(), Self::Error>> + Send;
fn clear_flt_update(
&mut self,
) -> impl Future<Output = Result<bool, UpdateError<Self::Error>>> + Send;
}
impl<T: SendTmdsScdc> TmdsScdc for T {
type Error = <T as SendTmdsScdc>::Error;
fn write_tmds_config(
&mut self,
config: SinkTmdsConfig,
) -> impl Future<Output = Result<(), Self::Error>> {
<T as SendTmdsScdc>::write_tmds_config(self, config)
}
fn update_tmds_config(
&mut self,
config: SinkTmdsConfig,
) -> impl Future<Output = Result<(), UpdateError<Self::Error>>> {
<T as SendTmdsScdc>::update_tmds_config(self, config)
}
fn read_tmds_config(&mut self) -> impl Future<Output = Result<TmdsConfigReading, Self::Error>> {
<T as SendTmdsScdc>::read_tmds_config(self)
}
fn read_scrambling_status(
&mut self,
) -> impl Future<Output = Result<ScramblingStatus, Self::Error>> {
<T as SendTmdsScdc>::read_scrambling_status(self)
}
fn read_sink_version(&mut self) -> impl Future<Output = Result<u8, Self::Error>> {
<T as SendTmdsScdc>::read_sink_version(self)
}
fn write_source_version(
&mut self,
version: u8,
) -> impl Future<Output = Result<(), Self::Error>> {
<T as SendTmdsScdc>::write_source_version(self, version)
}
fn clear_frl_rate(&mut self) -> impl Future<Output = Result<(), Self::Error>> {
<T as SendTmdsScdc>::clear_frl_rate(self)
}
fn clear_flt_update(&mut self) -> impl Future<Output = Result<bool, UpdateError<Self::Error>>> {
<T as SendTmdsScdc>::clear_flt_update(self)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::DdcErrorKind;
use core::pin::pin;
use core::task::{Context, Poll, Waker};
fn ready<F: Future>(future: F) -> F::Output {
let mut cx = Context::from_waker(Waker::noop());
match pin!(future).poll(&mut cx) {
Poll::Ready(output) => output,
Poll::Pending => panic!("the double's future waited"),
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum Call {
WriteTmdsConfig(SinkTmdsConfig),
UpdateTmdsConfig(SinkTmdsConfig),
ReadTmdsConfig,
ReadScramblingStatus,
ReadSinkVersion,
WriteSourceVersion(u8),
ClearFrlRate,
ClearFltUpdate,
}
struct Recorder {
calls: [Option<Call>; 8],
count: usize,
}
impl Recorder {
fn new() -> Self {
Self {
calls: [None; 8],
count: 0,
}
}
fn record(&mut self, call: Call) {
self.calls[self.count] = Some(call);
self.count += 1;
}
}
impl SendTmdsScdc for Recorder {
type Error = DdcErrorKind;
async fn write_tmds_config(&mut self, config: SinkTmdsConfig) -> Result<(), Self::Error> {
self.record(Call::WriteTmdsConfig(config));
Ok(())
}
async fn update_tmds_config(
&mut self,
config: SinkTmdsConfig,
) -> Result<(), UpdateError<Self::Error>> {
self.record(Call::UpdateTmdsConfig(config));
Err(UpdateError::Write(DdcErrorKind::Timeout))
}
async fn read_tmds_config(&mut self) -> Result<TmdsConfigReading, Self::Error> {
self.record(Call::ReadTmdsConfig);
Ok(TmdsConfigReading::new(
SinkTmdsConfig::new(true, false),
0xF1,
))
}
async fn read_scrambling_status(&mut self) -> Result<ScramblingStatus, Self::Error> {
self.record(Call::ReadScramblingStatus);
Ok(ScramblingStatus::new(true, 0x81))
}
async fn read_sink_version(&mut self) -> Result<u8, Self::Error> {
self.record(Call::ReadSinkVersion);
Ok(1)
}
async fn write_source_version(&mut self, version: u8) -> Result<(), Self::Error> {
self.record(Call::WriteSourceVersion(version));
Err(DdcErrorKind::Detached)
}
async fn clear_frl_rate(&mut self) -> Result<(), Self::Error> {
self.record(Call::ClearFrlRate);
Ok(())
}
async fn clear_flt_update(&mut self) -> Result<bool, UpdateError<Self::Error>> {
self.record(Call::ClearFltUpdate);
Ok(true)
}
}
fn exercise<S: TmdsScdc<Error = DdcErrorKind>>(scdc: &mut S) {
assert_eq!(
ready(scdc.write_tmds_config(SinkTmdsConfig::new(true, true))),
Ok(())
);
assert_eq!(
ready(scdc.update_tmds_config(SinkTmdsConfig::new(false, true))),
Err(UpdateError::Write(DdcErrorKind::Timeout))
);
let reading = ready(scdc.read_tmds_config()).unwrap();
assert_eq!(
(reading.config, reading.raw),
(SinkTmdsConfig::new(true, false), 0xF1)
);
let status = ready(scdc.read_scrambling_status()).unwrap();
assert_eq!((status.set, status.raw()), (true, 0x81));
assert_eq!(ready(scdc.read_sink_version()), Ok(1));
assert_eq!(
ready(scdc.write_source_version(1)),
Err(DdcErrorKind::Detached)
);
assert_eq!(ready(scdc.clear_frl_rate()), Ok(()));
assert_eq!(ready(scdc.clear_flt_update()), Ok(true));
}
#[test]
fn every_call_through_the_plain_trait_reaches_the_twin() {
let mut scdc = Recorder::new();
exercise(&mut scdc);
assert_eq!(
scdc.calls,
[
Some(Call::WriteTmdsConfig(SinkTmdsConfig::new(true, true))),
Some(Call::UpdateTmdsConfig(SinkTmdsConfig::new(false, true))),
Some(Call::ReadTmdsConfig),
Some(Call::ReadScramblingStatus),
Some(Call::ReadSinkVersion),
Some(Call::WriteSourceVersion(1)),
Some(Call::ClearFrlRate),
Some(Call::ClearFltUpdate),
]
);
}
}