tool: Support for owned and unowned generic Ec

This commit is contained in:
Ian Douglas Scott 2021-02-18 13:40:42 -08:00 committed by Jeremy Soller
parent f4458aebca
commit 0c1584385c
2 changed files with 37 additions and 0 deletions

View File

@ -24,3 +24,23 @@ pub trait Access {
/// The maximum size that can be provided for the data argument /// The maximum size that can be provided for the data argument
fn data_size(&self) -> usize; fn data_size(&self) -> usize;
} }
impl Access for &mut dyn Access {
unsafe fn command(&mut self, cmd: u8, data: &mut [u8]) -> Result<u8, Error> {
(**self).command(cmd, data)
}
fn data_size(&self) -> usize {
(**self).data_size()
}
}
impl Access for Box<dyn Access> {
unsafe fn command(&mut self, cmd: u8, data: &mut [u8]) -> Result<u8, Error> {
(**self).command(cmd, data)
}
fn data_size(&self) -> usize {
(**self).data_size()
}
}

View File

@ -244,6 +244,23 @@ impl<A: Access> Ec<A> {
]; ];
self.command(Cmd::LedSetColor, &mut data) self.command(Cmd::LedSetColor, &mut data)
} }
pub fn as_dyn(&mut self) -> Ec<&mut dyn Access> {
Ec {
access: &mut self.access,
version: self.version,
}
}
pub fn into_dyn(self) -> Ec<Box<dyn Access>>
where A: 'static {
Ec {
access: Box::new(self.access),
version: self.version,
}
}
} }
pub struct EcSpi<'a, A: Access> { pub struct EcSpi<'a, A: Access> {